user1300090
user1300090

Reputation: 13

How to find out rowindex when fire dropdown event inside gridview

I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview. Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown. so how to findout row number when fire dropdown's event.

Upvotes: 1

Views: 6396

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460098

I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   DropDownList ddl = (DropDownList)sender;
   GridViewRow row = (GridViewRow)ddl.NamingContainer;
   int rowIndex = row.RowIndex;
   // if you want to get the reference to the other DropDown in this row
   DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}

Upvotes: 5

4b0
4b0

Reputation: 22323

You can handle SelectedIndexChanged event of DropDownList.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
        GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
        string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}

Upvotes: 1

Related Questions