In this article I will explain with an example, how to get the CommandName, CommandArgument, RowIndex and the GridView Row inside RowCommand event when using ButtonField and Click event when using Button, LinkButton and ImageButton inside TemplateField column of ASP.Net GridView using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView control with three BoundField columns, a ButtonField column and a TemplateField column.
The GridView has been specified with a RowCommand event handler which will be triggered when the Button of the ButtonField column is clicked.
The TemplateField column consists of a Button control specified with Click event handler, CommandName and CommandArgument properties.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="OnRowCommand">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:ButtonField Text="Select Row" CommandName="Button_Command" />
        <asp:TemplateField>
            <ItemTemplate>
            <asp:Button ID = "btnSelect" runat = "server" Text = "Select Row"
            OnClick = "Row_Selected" CommandName = '<%# Eval("Name") %>' CommandArgument = '<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
I have created a dynamic DataTable with some dummy data and it is used to populate the GridView control in the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Get CommandName, CommandArgument, RowIndex and the GridView Row inside RowCommand event
Inside the RowCommand event handler, first the CommandName is fetched and then using the CommandArgument property, the RowIndex of the GridView Row is determined.
Finally using the RowIndex, the GridView Row reference is determined.
C#
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    //Get the Command Name.
    string commandName = e.CommandName;
 
    //Get the Row Index.
    int rowIndex = Convert.ToInt32(e.CommandArgument);
 
    //Get the Row reference in which Button was clicked.
    GridViewRow row = GridView1.Rows[rowIndex];
}
 
VB.Net
Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    'Get the Command Name.
    Dim commandName As String = e.CommandName
    'Get the Row Index.
    Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
    'Get the Row reference in which Button was clicked.
    Dim row As GridViewRow = GridView1.Rows(rowIndex)
End Sub
 
 
Get CommandName, CommandArgument, RowIndex and the GridView Row inside Button Click event
Inside the Click event handler of the Button, first the Button which was clicked is referenced using the sender object.
Then using the Button reference, the CommandName and CommandArgument properties are fetched.
The GridView Row reference is determined by making use of the NamaingContainer property of the Button.
Finally using the GridView Row reference, the RowIndex is determined.
C#
protected void Row_Selected(object sender, EventArgs e)
{
    //Get the Button reference.
    Button btnSelect = (sender as Button);
 
    //Get the Command Name.
    string commandName = btnSelect.CommandName;
 
    //Get the Command Argument.
    string commandArgument = btnSelect.CommandArgument;
 
    //Get the Row reference in which Button was clicked.
    GridViewRow row = (btnSelect.NamingContainer as GridViewRow);
 
    //Get the Row Index.
    int rowIndex = row.RowIndex;
}
 
VB.Net
Protected Sub Row_Selected(ByVal sender As Object, ByVal e As EventArgs)
    'Get the Button reference.
    Dim btnSelect As Button = CType(sender, Button)
    'Get the Command Name.
    Dim commandName As String = btnSelect.CommandName
    'Get the Command Argument.
    Dim commandArgument As String = btnSelect.CommandArgument
    'Get the Row reference in which Button was clicked.
    Dim row As GridViewRow = CType(btnSelect.NamingContainer, GridViewRow)
    'Get the Row Index.
    Dim rowIndex As Integer = row.RowIndex
End Sub
 
 
Downloads