How To Fill DropDown in GridView

Fill DropDown in GridView


In this article I’m going to explain how to use DropDownList control in ASP.NET using C#.
  ASP.NET allows you to use any server controls in GridView. In this article I’ll show you how
to use DropDownList control in GridView.
          DropDownList can be used to select only one item at a time item. It’s very easy to use DropDownList control
in GridView. We need to use OnRowDataBound event to bind DropDownList.
Description: http://www.dotnetfox.com/Document/1117/0photo3.jpg

Table Design (Employee Details):
Column Name
Data Type
empid
varchar(50)
name
varchar(100)
designation
varchar(50)
city
varchar(50)
country
varchar(50)
Create table script:
CREATE TABLE [dbo].[EmployeeDetails](
          [empid] [varchar](50) NULL,
          [name] [varchar](100) NULL,
          [designation] [varchar](100) NULL,
          [city] [varchar](50) NULL,
          [country] [varchar](50) NULL
) ON [PRIMARY]

Table Design (Designation):
Column Name
Data Type
id
int
designation
varchar(100)
Create table script:
CREATE TABLE [dbo].[Designation](
          [id] [int] NULL,
          [designation] [varchar](50) NULL
) ON [PRIMARY]

Designer Source Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvEmployeeDetails" runat="server" Width="100%"
 AutoGenerateColumns="false"
            ShowFooter="true" OnRowCommand="gvEmployeeDetails_RowCommand" 
OnRowDataBound="gvEmployeeDetails_OnRowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Employee ID">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpID" runat="server" Text=
'<%#DataBinder.Eval(Container.DataItem, "empid") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server" Text=
'<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:Label ID="lblDesignation" runat="server" 
Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlDesignation" runat="server">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" 
Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCity" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="lblCountry" runat="server"
 Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCountry" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action">
                    <FooterTemplate>
                        <asp:LinkButton ID="lbtnAdd" runat="server" 
CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html> 
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
   SqlConnection conn = new SqlConnection("Data Source=SPIDER;
Initial Catalog=Demo;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
     protected void BindData()
    {
        DataSet ds = new DataSet();    
        conn.Open();
        string cmdstr = "Select * from EmployeeDetails";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        cmd.ExecuteNonQuery();
        conn.Close();
        gvEmployeeDetails.DataSource = ds;
        gvEmployeeDetails.DataBind();       
    }

    protected void gvEmployeeDetails_RowCommand(object sender,
 GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("ADD"))
        {
            TextBox txtAddEmpID = (TextBox)gvEmployeeDetails.FooterRow.
FindControl("txtAddEmpID");
            TextBox txtAddName = (TextBox)gvEmployeeDetails.FooterRow.
FindControl("txtAddName");
            DropDownList ddlDesignation = (DropDownList)gvEmployeeDetails.
FooterRow.FindControl("ddlDesignation");
            TextBox txtAddCity = (TextBox)gvEmployeeDetails.FooterRow.
FindControl("txtAddCity");
            TextBox txtAddCountry = (TextBox)gvEmployeeDetails.FooterRow.
FindControl("txtAddCountry");
          
            conn.Open();
            string cmdstr = "insert into EmployeeDetails(empid,name,designation,city,country)
values(@empid,@name,@designation,@city,@country)";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
            cmd.Parameters.AddWithValue("@name", txtAddName.Text);
            cmd.Parameters.AddWithValue("@designation",
ddlDesignation.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
            cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);
            cmd.ExecuteNonQuery();
            conn.Close();
            BindData();
        }     
    }   
    protected void gvEmployeeDetails_OnRowDataBound(object sender,
 GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList ddlDesignation = (DropDownList)e.Row.FindControl("ddlDesignation");
            DataSet ds = new DataSet();
            conn.Open();
            string cmdstr = "Select * from Designation";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);           
            ddlDesignation.DataSource = ds.Tables[0];
            ddlDesignation.DataTextField = "designation";
            ddlDesignation.DataValueField = "id";
            ddlDesignation.DataBind();
            ddlDesignation.Items.Insert(0, new ListItem("--Select--""0"));
            conn.Close();
        } 
    }
}



No comments:

Post a Comment

Thank You For Your Great Contribution

Featured Post

Sql dynamic query With Join And search using like query with multiple column

Sql dynamic query With Join And search using like query with multiple column declare @keyword varchar(100) DECLARE @sqlCommand va...

Popular Posts