9

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.

Rajaram Shelar
  • 6,855
  • 24
  • 60
  • 101

1 Answers1

18

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:

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • I want index of the row which causing postback – Rajaram Shelar Nov 15 '12 at 09:39
  • here is a to find row index on row command link http://stackoverflow.com/questions/6503339/get-row-index-on-asp-net-rowcommand-event – शेखर Nov 15 '12 at 09:41
  • Yes i saw this, thanx for the responses but i want something more. I have gone through this. But i do not want to invoke any method. Just want to simpliest way to get selectedindex of the gridview – Rajaram Shelar Nov 15 '12 at 09:46
  • on the basis of that index i want to format(design) the row and also change the data sources for the downstairs in that row. – Rajaram Shelar Nov 15 '12 at 09:50
  • yes i can get the solution by your way but it requires calling separate function, from this getting naming container and then find row index, i want same index without calling this or i need to know is there any way which directly gives me the selectedindex – Rajaram Shelar Nov 15 '12 at 09:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19574/discussion-between-eraj-and-tim-schmelter) – Rajaram Shelar Nov 15 '12 at 10:01
  • @dran: i have deleted my comments to clean this question from unnecessary "noises". Maybe you wantel to delete yours also. – Tim Schmelter Nov 18 '13 at 17:27