In this article I will explain how to get data (values) of GridView Row in GridView RowCommand event in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with one BoundField column and one TemplateField column containing a TextBox. The GridView also contains a Command Button and is assigned OnRowCommand event handler.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150px" />
        <asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" />
    </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 data (values) of GridView Row in GridView RowCommand event in ASP.Net
The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is determined.
The TextBox control is referenced using the FindControl method of the GridView Row by passing the ID of the control as parameter. FindControl method returns the TextBox as an object of Control class and hence it has to be type casted to its respective type in order to access its properties.
The value of Name column is fetched from the TextBox, while the value of Country column is fetched from the GridView Cell based on the Column Index.
The name and country values are displayed using JavaScript Alert message box.
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //Determine the RowIndex of the Row whose Button was clicked.
    int rowIndex = Convert.ToInt32(e.CommandArgument);
 
    //Reference the GridView Row.
    GridViewRow row = GridView1.Rows[rowIndex];
 
    //Fetch value of Name.
    string name = (row.FindControl("txtName") as TextBox).Text;
 
    //Fetch value of Country
    string country = GridView1.Rows[rowIndex].Cells[1].Text;
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + name + "\\nCountry: " + country + "');", true);
}
 
VB.Net
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
    'Determine the RowIndex of the Row whose Button was clicked.
    Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
 
    'Reference the GridView Row.
    Dim row As GridViewRow = GridView1.Rows(rowIndex)
 
    'Fetch value of Name.
    Dim name As String = TryCast(row.FindControl("txtName"), TextBox).Text
 
    'Fetch value of Country
   Dim country As String = GridView1.Rows(rowIndex).Cells(1).Text
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Name: " & name & "\nCountry: " & country + "');", True)
End Sub
 
 
Screenshot
Get data (values) of GridView Row in GridView RowCommand event in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads