Populate DropDownList inside GridView/DataGrid in ASP.Net (Viewed - 5755 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

....
....
<asp:TemplateField>
    <HeaderTemplate>Emp</HeaderTemplate>
    <ItemTemplate ><asp:DropDownList ID="ddl1" runat ="server" OnLoad="ddl1_load"></asp:DropDownList></ItemTemplate>
</asp:TemplateField>
....
....


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();
    da.Fill(dt);
    DropDownList ddl = (DropDownList)sender;
    ddl.DataSource = dt;
    ddl.DataTextField = "EmpName";
    ddl.DataValueField = "EmpId";
    ddl.DataBind();
}
.....
.....


Post Article  Post Code 
  Post Tip    Start Forum
 
Quick note for developers
Google+
Follow