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.

 

Published Wednesday, September 22, 2010 2:00 PM by guru_sarkar
Filed under: ,

Comments

# re: Get GridView RowIndex upon button click

Friday, October 07, 2011 10:37 AM by Death259

धन्यवाद

# re: Get GridView RowIndex upon button click

Friday, January 06, 2012 1:00 AM by Jagadeesh Bollabathini

U showed an example the button which

is inside the gridview

But How do you solve which is outside the gridview

Please help me

Thank You

# re: Get GridView RowIndex upon button click

Thursday, January 19, 2012 12:17 PM by guru_sarkar

@Jagadeesh,

Sorry for late reply. I am not getting your question and wondering if it is related to this post and gridview rowid.

# re: Get GridView RowIndex upon button click

Sunday, February 12, 2012 10:02 AM by Wilson

Thank you so much!!

# re: Get GridView RowIndex upon button click

Tuesday, March 27, 2012 4:27 PM by chris

this helped me enormously. thanks.

# re: Get GridView RowIndex upon button click

Wednesday, June 20, 2012 2:02 PM by Sajjad Raza

Very informative and quick solution. thnx :)

# re: Get GridView RowIndex upon button click

Wednesday, July 04, 2012 10:30 AM by Steve

Thank you very very much, you saved my day.

# re: Get GridView RowIndex upon button click

Tuesday, July 17, 2012 11:56 AM by Anusuya

That worked perfectly and was the perfect solution to my problem. Thank you so much!

# re: Get GridView RowIndex upon button click

Friday, July 27, 2012 8:47 AM by Himanshu

mashalla it solved a great problem thanx a lot

# re: Get GridView RowIndex upon button click

Thursday, September 06, 2012 3:15 AM by vijay jd

Thank dear... keep it up..

# re: Get GridView RowIndex upon button click

Saturday, October 20, 2012 5:58 AM by chandran.nr

U showed an example the button which

is inside the gridview

But How do you solve which is outside the gridview

Please help me

My email id is chandran.nr@gmail.com

Thank You

# re: Get GridView RowIndex upon button click

Wednesday, November 21, 2012 9:46 AM by RonyC

chandran,

He mentioned this also in point 2:

'<%# Container.DataItemIndex %>'.

You can access the DataItemIndex prop of the row.

Suppose you have a button outside the gridView. In the click event:

protected void btnSelectedItems_Click(object sender, EventArgs e)

{

  foreach (GridViewRow row in GridView.Rows)

  {

    //This gives you the rowIndex = the index of current item.

    System.Diagnostics.Debug.WriteLine(row.DataItemIndex);

  }

}

Leave a Comment

(required) 
(required) 
(optional)
(required)