In this article I have explained with an example, how to get the selected row of GridView on Button click in ASP.Net using C# and VB.Net.
The GridView Row is selected using the Select Button and then the values are fetched from the Selected Row Columns.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with one BoundField column, one TemplateField column with a Label and one ButtonField column which consists of a Select Button to select the GridView Row.
There is also a Label control below the GridView for displaying the Selected Row Cell values.
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    AutoGenerateColumns="false" OnSelectedIndexChanged = "OnSelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:ButtonField Text="Select" CommandName="Select" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<br />
<u>Selected Row Values: </u>
<br />
<br />
<asp:Label ID="lblValues" runat="server" Text=""></asp:Label>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside 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 selected row in GridView on Button Click in ASP.Net
Inside the SelectedIndexChanged event handler, the BoundField Cell value is extracted using the Cells property.
For extracting the value of the TemplateField Cell, first the Label control is referenced and then the value is extracted.
Finally, both the values are displayed in Label control.
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    //Accessing BoundField Column.
    string name = GridView1.SelectedRow.Cells[0].Text;
 
    //Accessing TemplateField Column controls.
    string country = (GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;
 
    lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
}
 
VB.Net
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
    'Accessing BoundField Column.
    Dim name As String = GridView1.SelectedRow.Cells(0).Text
 
    'Accessing TemplateField Column controls.
    Dim country As String = TryCast(GridView1.SelectedRow.FindControl("lblCountry"), Label).Text
 
    lblValues.Text = "<b>Name:</b> " & name & " <b>Country:</b> " & country
End Sub
 
 
Screenshot
Get selected row in GridView on Button Click in ASP.Net
 
 
Demo
 
 
Downloads