Hello,
I can't dynamically populate a DropDownList within a GridView using DataTable in C# ASP NET
There are no errors but the list is empty.
My code below
private void BindGrid()
{
DataTable dt = new DataTable();
DropDownList ddlEMP = new DropDownList();
dt.Columns.Add("id", typeof(Int32));
dt.Columns.Add("Name", typeof(string));
DataRow dtrow = dt.NewRow();
dtrow["Id"] = 1;
dtrow["Name"] = "FIG";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["Id"] = 2;
dtrow["Name"] = "CDE";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["Id"] = 3;
dtrow["Name"] = "ABC";
dt.Rows.Add(dtrow);
dtrow = dt.NewRow();
dtrow["Id"] = 4;
dtrow["Name"] = "Ram";
dt.Rows.Add(dtrow);
ddlEMP.DataSource = dt;
ddlEMP.DataTextField = "NAME";
ddlEMP.DataValueField = "Id";
ddlEMP.Items.Insert(0, new ListItem("Select", "0"));
ddlEMP.DataBind();
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
<asp:GridView ID="gvProducts"
runat="server"
DataKeyNames="Id"
EmptyDataText=""
EnableViewState="true"
CssClass="mGrid"
HorizontalAlign="Center"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" ReadOnly="true" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" />
<asp:TemplateField
HeaderText="DDL"
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlEMP">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>