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.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Binding the GridView with records from SQL Server Database Table
Inside the Page Load event handler, the GridView is populated using DataTable with records from the SQL Server database.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string query = "SELECT TOP 5 CustomerId,PostalCode,City,Country FROM Customers";
 
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView2.DataSource = dt;
                GridView2.DataBind();
                GridView3.DataSource = dt;
                GridView3.DataBind();
                GridView4.DataSource = dt;
                GridView4.DataBind();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handles me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim query As String = "SELECT TOP 5 CustomerId,PostalCode,City,Country FROM Customers"
 
        Using con As SqlConnection = New SqlConnection(constr)
            Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
                sda.Fill(dt)
                GridView1.DataSource = dt
                GridView1.DataBind()
                GridView2.DataSource = dt
                GridView2.DataBind()
                GridView3.DataSource = dt
                GridView3.DataBind()
                GridView4.DataSource = dt
                GridView4.DataBind()
            End Using
        End Using
    End If
End Sub
 
 
Getting GridView Row and GridView Row Index inside RowCommand Event
HTML Markup
The HTML Markup consists of:
GridView – For displaying data.
Columns
GridView consists of one ButtonField column.
Events:
The GridView has been assigned with OnRowCommand event handler. When the Button is clicked, the OnRowCommand event is executed.
<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, 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
 
Screenshot
ASP.Net GridView - Get Row Index on RowCommand and Click events
 
 
Getting GridView Row and GridView Row Index on Button Click in TemplateField
HTML Markup
The HTML Markup consists of:
GridView – For displaying data.
Columns
GridView consists of one TemplateField column.
The TemplateField column consists of ItemTemplate with Button, and Button has been set with CommandArgument property.
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="GridView2" 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
When the Button is clicked, the GridView Row is referenced using the Parent property and then using the reference of the GridView Row, the RowIndex 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.
Note: 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
 
Screenshot
ASP.Net GridView - Get Row Index on RowCommand and Click events
 
 
Getting CommandArgument on Button Click in TemplateField
HTML Markup
The HTML Markup consists of:
GridView – For displaying data.
Columns
GridView consists of one TemplateField column.
The TemplateField column consists of ItemTemplate with Button, and Button has been set with CommandName and CommandArgument properties.
 
Events:
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="GridView3" 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
When the Button is clicked, the Button is referenced using the sender object and with the help of object reference the CommandName and CommandArgument properties are fetched.
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
 
Screenshot
ASP.Net GridView - Get Row Index on RowCommand and Click events
 
 
Getting multiple CommandArgument values on Button
HTML Markup
The HTML Markup consists of:
GridView – For displaying data.
Columns
GridView consists of one TemplateField column.
The TemplateField column consists of an ItemTemplate which contains a Button to which CommandArgument property has been set.
 
Properties:
CommandArgument – It is used to set Multiple values of a Button by concatenating multiple columns using separator(+).
 
Events:
The Button has been assigned with an OnClick event handler.
<asp:GridView ID="GridView4" 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="Button3_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Code
When the Button is clicked, it is referenced using the sender object and with the help of object referene the CommandArgument property is fetched.
Then, the value of the CommandArgument is split into multiples values using the Split function of String class.
C#
protected void Button3_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 Button3_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(","c)
   Dim commandArgument1 As String = commandArgument(0)
   Dim commandArgument2 As String = commandArgument(1)
   Dim commandArgument3 As String = commandArgument(2)
End Sub
 
Screenshot
ASP.Net GridView - Get Row Index on RowCommand and Click events
 
 
Downloads