views:

150

answers:

2

I have a Datagrid in image below.

[url=http://www.freeimagehosting.net/][img=http://www.freeimagehosting.net/uploads/60d1ac6210.jpg][/url]

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.

+1  A: 

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.

Mitchel Sellers
+1  A: 

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