I have a dropdownlist control in a datagrid template field. In the SelectedIndexChanged event, I just want to get the position of the sender object in order to create a reference to the row the sender object was in. All I’ve found on google is how to loop through each row of the datagrid to compare it to sender’s client ID to see if it is in fact my selected row. Why can’t I just get the position of the sender object and just use it to create an instance of that gridviewrow? And why doesn’t the below work?
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.SelectedRow;
var store = row.Cells[0].Text; //I get the Object reference not set to an instance of an object error here
}
Am I missing something?
The
senderis the DropDownList. Its NamingContainer is theGridViewRow. This has a property RowIndex. I assume that this (or the row) is what you want. Here is both: