Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get index of the gridview row

Tags:

asp.net

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.

like image 403
Rajaram Shelar Avatar asked Nov 15 '12 09:11

Rajaram Shelar


People also ask

What is e RowIndex in C#?

As the object suggests (GridViewUpdateEventArgs) 'e' stands for the events relating to the update of a grid view. You can get similar method signatures that relate to other events such as deletions etc. The 'RowIndex' relates to the index of the row on which this event was fired.

What is RowCommand event in GridView?

The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.


1 Answers

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
like image 188
Tim Schmelter Avatar answered Oct 16 '22 20:10

Tim Schmelter