Nahid Nahid - 1 year ago 98
ASP.NET (C#) Question

Can't find row index in Datagridview in ASP.NET?

I can't find row Index in Datagridview when I call a event in DropDownList.

The code is:

protected void DescriptionOfProduct_SelectedIndexChanged(object sender, EventArgs e)
DropDownList _DescriptionOfProduct = (DropDownList)gvSales.FindControl("DescriptionOfProduct");

Label _Unit = (Label)gvSales.FindControl("Unit");
Label _PriceType = (Label)gvSales.FindControl("PriceType");

Please give me solution.

Answer Source

If the dropdownlist is inside of the gridview, then you can find the row by realizing that it's in a cell within a row. So, the parent of its parent is therefore the row.

GridViewRow currentRow = (GridViewRow)_DescriptionOfProduct.Parent.Parent;
int index = currentRow.RowIndex;


If you're still having an issue with this, I'd suggest what Mitchel Sellers said in his answer. Find your dropdownlist by casting the sender as a dropdownlist. Then you can find your row. From there, you can find each control by using the row's cells to grab the controls.

Here is an example of how I've done this before...

protected void actionList_SelectedIndexChanged(object sender, EventArgs e)
        DropDownList list = (DropDownList)sender;
        GridViewRow row = (GridViewRow)list.Parent.Parent;
        Label id = (Label)row.Cells[0].Controls[1];        

        // etc......
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download