binding drop down lists in editable datagrids
-
I have an editable datagrid with some textboxes and a drop down list in editable mode. The thing is that I know how to bind data to the textboxes so that when ever I click the edit button on the datagrid the textboxes appear with the default data of the correspondiong cell. However I'm stuck with trying to implement the same for the drop down list. I would appreciate any form of help on this. :-D
-
I have an editable datagrid with some textboxes and a drop down list in editable mode. The thing is that I know how to bind data to the textboxes so that when ever I click the edit button on the datagrid the textboxes appear with the default data of the correspondiong cell. However I'm stuck with trying to implement the same for the drop down list. I would appreciate any form of help on this. :-D
Hi there, To use a dropdownlist control when the datagrid is set in edit mode, you simply use a template column and specify a dropdownlist control in the EditItemTemplate element. To bind data to the dropdownlist, you either specify the
DataSource
property at design time:asp:TemplateColumn
...
<EditItemTemplate>
<asp:DropDownList Runat="server" ID="DropDownList1" DataSource="<%# DDLDataSource() %>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>//The sample code in code-behind
public string[] DDLDataSource()
{
string[] source = new string[3]{"Manager", "Superuser", "User"};return source;
}
or do that in code at runtime, the sample code looks like this:
private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{DataGrid1.EditItemIndex = e.Item.ItemIndex; //You code here to rebind the DataGrid. ... //I assume you want to use a dropdownlist for the first column DataGridItem item = DataGrid1.Items\[e.Item.ItemIndex\]; DropDownList ddl = (DropDownList)item.Cells\[1\].FindControl("DropDownList1"); ddl.DataSource = DDLDataSource(); ddl.DataBind();
}