DataGrid Template w/ DropDownList
-
I have a simple drop down list located within a DataGrid Edit Template. I am having trouble trying to have the correct ListItem selected. In the Item template i am simply converting the 1 or 0 that i save in the database to a "Yes" or "No" for the users. ItemTemplate:
<%# bidtoolAdmin.library.Common.convertBooleanYesNo(DataBinder.Eval(Container.DataItem, "bln_activeFlag").ToString()).ToString() %>
EditTemplate:No Yes
I have tried the examples i've found in various articles with no luck:private void dgr_leadSources_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.EditItem) { DataRowView drv = (DataRowView) e.Item.DataItem; String currentFlag = drv["bln_activeFlag"].ToString(); DropDownList ddl = (DropDownList) e.Item.FindControl("ddl_activeFlag"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); } }
Please help to prevent my insanity! -
I have a simple drop down list located within a DataGrid Edit Template. I am having trouble trying to have the correct ListItem selected. In the Item template i am simply converting the 1 or 0 that i save in the database to a "Yes" or "No" for the users. ItemTemplate:
<%# bidtoolAdmin.library.Common.convertBooleanYesNo(DataBinder.Eval(Container.DataItem, "bln_activeFlag").ToString()).ToString() %>
EditTemplate:No Yes
I have tried the examples i've found in various articles with no luck:private void dgr_leadSources_ItemDataBound(object sender, DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.EditItem) { DataRowView drv = (DataRowView) e.Item.DataItem; String currentFlag = drv["bln_activeFlag"].ToString(); DropDownList ddl = (DropDownList) e.Item.FindControl("ddl_activeFlag"); ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); } }
Please help to prevent my insanity!Instead of: ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); You might try: ddl.Items.FindByValue(currentFlag).Selected = true; (will give an error though if it doesn't find an item matching your currentFlag) Marcie http://www.codeproject.com
-
Instead of: ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(currentFlag)); You might try: ddl.Items.FindByValue(currentFlag).Selected = true; (will give an error though if it doesn't find an item matching your currentFlag) Marcie http://www.codeproject.com
-
I'm getting an error before it gets to that line... Specified cast is not valid
Line 119: DataRowView drv = (DataRowView) e.Item.DataItem;
Aha. Your DataItem will only be of type DataRowView if you're binding to a DataSet. For DataReaders, see this post: http://weblogs.asp.net/datagridgirl/archive/2003/03/13/3760.aspx Marcie http://www.codeproject.com
-
Aha. Your DataItem will only be of type DataRowView if you're binding to a DataSet. For DataReaders, see this post: http://weblogs.asp.net/datagridgirl/archive/2003/03/13/3760.aspx Marcie http://www.codeproject.com