Setting the SelectedIndex of a DropDownList in a Datagrid

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.
 
A

Ashish

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
 
A

Andrew de la Harpe

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
 
A

Andrew de la Harpe

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..

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top