populating dropdownlist within a datagrid
-
I wonder how to do in order to populate a dropdownlist (with data from database) within a datagrid on page Load. Notice that I want the dropdownlists to be populated within the datagrid without that the datagrid is in edit mode.Any example code? Thanks.
-
I wonder how to do in order to populate a dropdownlist (with data from database) within a datagrid on page Load. Notice that I want the dropdownlists to be populated within the datagrid without that the datagrid is in edit mode.Any example code? Thanks.
-
I wonder how to do in order to populate a dropdownlist (with data from database) within a datagrid on page Load. Notice that I want the dropdownlists to be populated within the datagrid without that the datagrid is in edit mode.Any example code? Thanks.
In order to populate a dropdownlist with in a datagrid. Create a template coloumn and add a DropDownlist in
ItemTemplate
section. To bind data SpecifyDataTextField
DataValueField
DataSource
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False" Width="184px" Height="176px">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Designation">
<ItemTemplate>
<asp:DropDownList id=ddlList runat="server" Width="184px"
DataSource="<%#PopulateList()%>" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>An alternate way to do using code is by mapping
ItemDataBound
Eventprivate void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DropDownList ddlLst = (DropDownList) e.Item.FindControl("ddlList");
ddlLst.DataValueField = "ID";
ddlLst.DataTextField = "Name";
ddlLst.DataSource = PopulateList();
ddlLst.DataBind();
}
}