Populate DropDownList inside GridView/DataGrid in ASP.Net (Viewed - 5812 times)

Here is the code to explain you hou to bind the DropDownList present inside GridView or DataGrid in ASP.Net (Csharp as code behind) add one dropdownlist in templatefield in GridView make sure you add OnLoad event this event will fire for each row in gridview

    <ItemTemplate ><asp:DropDownList ID="ddl1" runat ="server" OnLoad="ddl1_load"></asp:DropDownList></ItemTemplate>

Now in C# Code behind you can create OnLoad event handlar that will bind the data to the dropdown. In below example dropdown is bind with employee data.

protected void ddl1_load(object sender, EventArgs e)
    SqlDataAdapter da = new SqlDataAdapter("select emp from Employee", cn);
    DataTable dt = new DataTable();
    DropDownList ddl = (DropDownList)sender;
    ddl.DataSource = dt;
    ddl.DataTextField = "EmpName";
    ddl.DataValueField = "EmpId";

Post Article  Post Code 
  Post Tip    Start Forum
Quick note for developers