Retieving DataKeys Value From GridView Row

October 18, 2012

protected void DropDownListFoo_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the drop down list (Or any control that fired an event in the row we require the datakey from
    DropDownList dropDownListFoo = (DropDownList)sender as DropDownList;

    // Get the row the control was on (Its Naming Container)
    GridViewRow row = (GridViewRow)dropDownListFoo.NamingContainer as GridViewRow;

    // Get the DataKey value for the row
    int id = (int)GridViewFoo.DataKeys[row.RowIndex].Value;

    // Do something with id...
    DoSomething(id);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: