Md Nasir Uddin
Md Nasir Uddin

Reputation: 2180

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.

Upvotes: 1

Views: 763

Answers (2)

Aaron
Aaron

Reputation: 7541

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;

EDIT

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......
    }

Upvotes: 1

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

The sender of the event should be your dropdown list.

DropDownList myList = sender as DropDownList;
if(myList != null)
{
    //Now you have your selected index myList.SelectedIndex
}

If this goes to an else, the sender is showing as something else.

Upvotes: 1

Related Questions