In this article I will explain with an example, how to reference GridView Row and get GridView Row Index when Button, ImageButton and LinkButton inside the GridView control is clicked in ASP.Net using C# and VB.Net.
This article will also explain, how to set multiple CommandArgument properties in GridView control and also how to fetch the values of multiple CommandArgument properties inside the Button click events.
 
 
Get GridView Row and GridView Row Index inside RowCommand Event
HTML Markup
The HTML markup consist of a simple ASP.Net GridView Control with a ButtonField and assigned with OnRowCommand event.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    OnRowCommand="OnRowCommand">
 <Columns>
  <asp:ButtonField CommandName="ButtonField" DataTextField="CustomerID"
        ButtonType="Button"/>
 </Columns>
</asp:GridView> 
 
Code
Inside the OnRowCommand event handler of the ASP.Net GridView, first the RowIndex is fetched from the CommandArgument property and then using the RowIndex, the GridView Row reference is determined.
C#
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index]; 
}
 
VB.Net
Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim gvRow As GridViewRow = GridView1.Rows(index)
End Sub
 
 
Get GridView Row and GridView Row Index on Click event of Button in TemplateField
HTML Markup
The HTML markup consist of a simple ASP.Net GridView Control with a Button inside TemplateField which is assigned with an OnClick event.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
           <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
                  CommandArgument="Button1" OnClick="Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Code
Inside the OnClick event handler of the Button, first the GridView Row is referenced using the Parent property and then using the reference of the GridView Row, the RowIndex, the GridView Row is determined.
Here sender is the reference of the Button which was clicked, its parent is the GridView Cell and parent of the GridView Cell is the GridView Row.
This same approach can be used for LinkButton and ImageButton.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
    int index = gvRow.RowIndex;
}
 
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim gvRow As GridViewRow = CType(CType(sender, Control).Parent.Parent, _
                                        GridViewRow)
    Dim index As Integer = gvRow.RowIndex
End Sub
 
 
Get CommandArgument on Click event of Button in TemplateField
 HTML Markup
The HTML markup consist of a simple ASP.Net GridView Control with a Button inside TemplateField which is assigned with an OnClick event.
The Button has been set with a CommandName and CommandArgument properties.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
           <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>' CommandName="Button1_Name" CommandArgument="Button1_Argument" OnClick="Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Code
Inside the Button Click event handler, first the Button is referenced using the sender object and then from the Button object the CommandName and CommandArgument properties are fetched.
This same approach can be used for LinkButton and ImageButton.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string CommandName = btn.CommandName;
    string CommandArgument = btn.CommandArgument;
}
 
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim btn As Button = CType(sender, Button)
   Dim CommandName As String = btn.CommandName
   Dim CommandArgument As String = btn.CommandArgument
End Sub   
 
 
Button CommandArgument with multiple values
HTML Markup
The HTML markup consist of a simple ASP.Net GridView Control with a Button inside TemplateField which is assigned with an OnClick event.
Multiple values can be set in the CommandArgument property of a Button by concatenating multiple values using a separator.
Later the values can be separated out by splitting the concatenated string using the Split function of String class.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
         <asp:Button ID="Button1" runat="server" Text='<%#Eval("CustomerID")%>'
          CommandArgument=
          '<%#Eval("PostalCode") + "," + Eval("City") + "," + Eval("Country") %>' 
              OnClick="Button1_Click" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Code
Inside the Button Click event handler, first the Button is referenced using the sender object and then from the Button object the CommandArgument property is fetched.
The value of the CommandArgument is split into multiples values using the Split function of String class.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    string[] CommandArgument = btn.CommandArgument.Split(',');
    string CommandArgument1 = CommandArgument[0];
    string CommandArgument2 = CommandArgument[1];
    string CommandArgument3 = CommandArgument[2];
}
 
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim btn As Button = CType(sender, Button)
   Dim CommandName As String = btn.CommandName
   Dim CommandArgument As String() = btn.CommandArgument.Split(",")
   Dim CommandArgument1 As String = CommandArgument(0)
   Dim CommandArgument2 As String = CommandArgument(1)
   Dim CommandArgument3 As String = CommandArgument(2)
End Sub