How to get index of the gridview row

asp.net

Solution

You can get the index of the row in `RowDataBound` via the `RowIndex` property:

protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int index = e.Row.RowIndex;
    }
}

You can get the index of any child control in a GridView via

- `childControl.NamingContainer` =>

- `GridViewRow` =>

- `RowIndex`

for example in a `DropDownList.SelectedIndexChanged` event:

protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList) sender;
    GridViewRow row  = (GridViewRow)  ddl.NamingContainer;
    int index        = row.RowIndex;
}

Finally: since you've mentioned " to check selectedrow index", if you're actually looking for the selected row index of the `GridView` itself, there's a property just for this:

- `GridView.SelectedIndex` Property

Problem

How can i get row index of the grid view whose child control is doing post back. I know the way of getting control causing post back and finding its parent container which returns grid view row and then find its index. I want this in a RowDataBound event to check selectedrow index and formatting the same. Is there any other property or settings which directly emits selected index of the grid view on any child control post back Edit: For example if there is a dropdownlist in a row and there are certain rows in a gridview. Then i want gridview rowindex where postback happens due to that dropdowlist in the gridview row.

Original source

Related problems