How to Get Selected Row Values From a Gridview Control in Asp.Net C# VB.Net

For implementing more business logic some times we need to capture the data of GridView Selected Row. The event named SelectedIndexChanged provides us the easiest way to get selected row data. Because when user click on the Select Button the SelectedIndexChanged event will fire. Here in this article I will explain “How one can Get Selected Row Values From a Gridview Control in Asp.Net“. We will describe in both C# & VB.Net.

Objective/Outcome:
How to Get Selected Row Values From a Gridview Control in Asp.Net C# VB.Net

Prepare the GridView HTML like below:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        DataKeyNames="ID" AutoGenerateSelectButton="True"  
        HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" 
        HeaderStyle-ForeColor="White" CellPadding="5" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name">
        </asp:BoundField>
        <asp:BoundField DataField="Address" HeaderText="Address">
        </asp:BoundField>
        <asp:BoundField DataField="Contact" HeaderText="Contact no">
        </asp:BoundField>
    </Columns>
    </asp:GridView>
    <br />
    <hr />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
    <asp:Label ID="Label3" runat="server" Text=""></asp:Label>

Bind the GridView like below:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("ID"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            GridView1.DataSource = dtSupplier;
            GridView1.DataBind();
        }
    }

Note: Don’t forget to add “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("ID"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")

            GridView1.DataSource = dtSupplier
            GridView1.DataBind()
        End If
    End Sub

Now Read GridView Selected Row Data:
C# Code:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Reading DataKeyName
        Label1.Text= "DataKeyName: "+GridView1.DataKeys[GridView1.SelectedRow.RowIndex].Value.ToString();
        
        // Reading from SelectedRow
        Label2.Text = "Name: " + GridView1.SelectedRow.Cells[1].Text;
        Label3.Text = "Name: " + GridView1.SelectedRow.Cells[2].Text;
    }

VB.Net Code:

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs)
        ' Reading DataKeyName
        Label1.Text = "DataKeyName: " & GridView1.DataKeys(GridView1.SelectedRow.RowIndex).Value.ToString()

        ' Reading from SelectedRow
        Label2.Text = "Name: " + GridView1.SelectedRow.Cells(1).Text
        Label3.Text = "Name: " + GridView1.SelectedRow.Cells(2).Text
    End Sub

Since you get the Datakeyname. Hope you can do everything.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code