Setting the SelectedIndex of a DropDownList in a Datagrid

  • Thread starter Thread starter K Bryan
  • Start date Start date
K

K Bryan

Hi There,

I have two datagrids on a webform both of which display DropDownLists when
edit mode is invoked.

That being said, does anyone have a generic or straightforward way in C# so
that when I go into the edit mode of a row, the dropdownlists display the
field values of that row? (They currently jump to the first value in the
dropdownlist)

Any help would be GREATLY appreciated,

_K.
 
K said:
Hi There,

I have two datagrids on a webform both of which display DropDownLists when
edit mode is invoked.

That being said, does anyone have a generic or straightforward way in C# so
that when I go into the edit mode of a row, the dropdownlists display the
field values of that row? (They currently jump to the first value in the
dropdownlist)

Any help would be GREATLY appreciated,

_K.

grab the dropdown list from the datagrid

something like

for each item in DataGrid1.Items
dim ddl as DropDownList = item.FindControl("DropDownList1")
ddl.selectedItemIndex = MyIndexValue
next

remember to do it after databinding has been done, and not in Item_create

you can do it at Item_DataBound,
should work

hth
-ashish
 
You need to find the ddl in the collection of controls.
So you do something like this.
DropDownList ddl = (DropDownList) e.Item.FindControl("lstID");

then select the correct item as required

A
 
In the OnDataBound event for the grid.
Heres as example.
private void setDataBinding(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)

{


ListItemType lit = e.Item.ItemType;

if(lit == ListItemType.EditItem)

{

DataRowView drv = (DataRowView) e.Item.DataItem;

DropDownList dll = (DropDownList) e.Item.FindControl("lstLaterability");

etc..

}

}
 
Back
Top