Thursday 18 April 2013

Working Dropdownlist inside the Gridview in ASP.NET

Working Dropdownlist inside the Gridview in ASP.NET

http://usingaspdotnet.blogspot.in/2011/03/dropdownlist-inside-gridview-in-aspnet.html

Introduction: Hello friends, in this article i will explain that how we can bind dropdownlist inside  the gridview. This is very interesting article for every .net developers. 

Implementation: Create a new website add a page named dd_inside_the_gridview.aspx. Drag and drop the gridview  at  this page from the toolbox. Below i am giving the html code for the .aspx page.

Code for dd_inside_the_gridview.aspx page:

<form id="form1" runat="server">
    <div>
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" ForeColor="Black" GridLines="Horizontal" Height="241px" 
        Width="374px">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <%#Eval("id")  %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <%#Eval("name")  %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="designation">
                    <ItemTemplate>
                        <asp:DropDownList ID="dd_desghnation" runat="server">
                            <asp:ListItem> --Select--</asp:ListItem>
                          
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
    </form>


Code for dd_inside_the_gridview.aspx.cs page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
// use above three namespaces for this article

public partial class dd_inside_the_gridview : System.Web.UI.Page
{
    DropDownList bind_dropdownlist;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack == false)
        {
            // here i am calling the function which bind the gridview
            bind_gridview_with_dropdownlist();
        }
    }
    private void bind_gridview_with_dropdownlist()
    {
        // here i am declare a connectionstring to attach the database 
        SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
        con.Open();
        // here i am definning the sql query which will used for the binding of the gridview and declare the SqlDataAdapter
        SqlDataAdapter adp = new SqlDataAdapter("select * from tb_designation", con);
        // here i am declare the dataset to fill the gridview
        DataSet ds = new DataSet();
        // here i am filling the SqlDataAdapter with the dataset
        adp.Fill(ds, "tb_designation");
        //here i am binding the gridview with the dataset ds  
        GridView1.DataSource = ds;
        GridView1.DataBind(); 
        // here i am using the foreach loop to bind the gridview with the database
        foreach (GridViewRow grdRow in GridView1.Rows)
        {
            // here i am definning the property of the DropDownList as bind_dropdownlist
           // DropDownList bind_dropdownlist = new DropDownList();
            // here i am finding the  DropDownList from the gridiew for        binding
            bind_dropdownlist = (DropDownList)(GridView1.Rows[grdRow.RowIndex].Cells[2].FindControl("dd_desghnation"));
            // here i am binding the DropDownList with the dataset ds
            bind_dropdownlist.DataSource = ds;
            // here i am set the DropDownList's DataValueField as id
            bind_dropdownlist.DataValueField = "ID";
            // here i am set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database
            bind_dropdownlist.DataTextField = "designation";
            bind_dropdownlist.DataBind();
        }
    }
}
Below i am displaying the outout of this program in the image:

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGMwHp7NxHKxb7CYmncbK1gsLlqw35QPUozsg8kIYuJ80NpCrH4Yv6NQz-qXkghyphenhyphenATB7Y40X7rBP7rtkGjRk-4FaVGI8SzerzGHwI3-2Qr_SsIQwjhQWyTNmGKmbqCkVlskdJtt7u1cT1A/s320/drop-down.jpg




SQl script for the table tb_designation:

/****** Object:  Table [dbo].[tb_designation]    Script Date: 03/08/2011 18:59:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_designation]') AND type in (N'U'))
DROP TABLE [dbo].[tb_designation]
GO
/****** Object:  Table [dbo].[tb_designation]    Script Date: 03/08/2011 18:59:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_designation]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tb_designation](
     [ID] [int] IDENTITY(1,1) NOT NULL,
     [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
     [designation] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
END
GO
SET IDENTITY_INSERT [dbo].[tb_designation] ON
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (1, N'Bharat', N'manager')
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (2, N'John', N'clerck')
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (3, N'vikas', N'director')
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (4, N'ajay', N'developer')
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (5, N'manoj', N'designer')
INSERT [dbo].[tb_designation] ([ID], [Name], [designation]) VALUES (6, N'vishal', N'content writer')
SET IDENTITY_INSERT [dbo].[tb_designation] OFF

No comments:

Post a Comment