Wednesday, June 9, 2010

Find row index in a gridview for a row after selecting dropdownlist item

I had a gridview with an item template that has dropdownlist in a
column. When a user changed the dropdownlist selection
I needed to update the record. Problem is the row was not selected so
how do I get the datakey?
On selected index changing you do it like this:

DropDownList myDropDown = (DropDownList)sender;
GridViewRow myRow = (GridViewRow)myDropDown.Parent.Parent;
String myDataKey =
Convert.ToString(myGridView.DataKeys[myRow.RowIndex].Value);

Now you can go do your updates, delete or whatever.

No comments: