Get GridView RowIndex upon button click

Scenario:

There might be case when you want to get the row-index or any other details from the row, when a button inside a GridView Row is clicked. I will show you different ways.

1:  "Select" link/button Click

It is very easy when to get row information when you click auto-generated "Select" link/button in GridView row. When this link/button is clicked, gridview raises "Select" event. GridView exposes SelectedRow property which represents the currently selected row and SelectedIndex property which gives the rowindex of the currently selected Row.

When you click the "Select" link/button, you might sometimes want to run custom code using RowIndex. You can do that easily by handling OnSelectedIndexChanged event and do your manipulation.

Now suppose it is not a "Select" button i.e. a button whose CommandName is not 'Select'. In that case you can use any of the following options.

2: Passing RowIndex as CommandArgument of the Button

a) Define you but inside a TemplateField.

b) Set it's CommandName property to some meaningful value

c) Set the CommandArgument of the button as shown in code below.

d) Then use OnRowCommand event to access the row as show in code below:

e.g.  here is a sample TemplateField that has a button and the corresponding OnRowCommand event handler

<asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="Button1" CommandName="MyButtonClick" CommandArgument='<%# Container.DataItemIndex %>'  runat="server" Text="MyButton" />
            <asp:Button ID="Button2" runat="server" Text="Button" />
        </ItemTemplate>
      </asp:TemplateField>
 protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MyButtonClick")
        {
            //Get rowindex
            int rowindex = Convert.ToInt32(e.CommandArgument);
            //Get Row
            GridViewRow gvr = GridView1.Rows[rowindex];
        }
    }

Point to note: CommandArgument='<%# Container.DataItemIndex %>'. Container.DataItemIndex gives the index of current item.

3: Use Button Click Event

 <asp:TemplateField>
        <ItemTemplate>
               <asp:Button ID="Button1" runat="server" Text="Button" OnClick="MyButtonClick" />
        </ItemTemplate>
</asp:TemplateField>  protected void MyButtonClick(object sender, System.EventArgs e)
    {
        //Get the button that raised the event
        Button btn = (Button)sender;

        //Get the row that contains this button
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;

        //Get rowindex
        int rowindex = gvr.RowIndex;
    }

Point to note:

The last method works with other controls like DropDownList, CheckBoxList, etc inside of gridview. e.g. In case of dropdownlist you will use the OnSelectedIndexChanged event  like shown below:

 protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        //Get the dropdownlist that raised the event
        DropDownList ddl = (DropDownList)sender;

        //Get the row that contains this dropdown
        GridViewRow gvr = (GridViewRow)ddl .NamingContainer;

        //Get rowindex
        int rowindex = gvr.RowIndex;
    } 

Hope this helps.

 

12 Comments

Comments have been disabled for this content.