开发者

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

开发者 https://www.devze.com 2022-12-20 12:51 出处:网络
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)

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 开发者_JAVA技巧_DescriptionOfProduct = (DropDownList)gvSales.FindControl("DescriptionOfProduct");

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

Please give me solution.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号