Get Selected Row Data in GridView on Button Click in ASP.Net

Get Selected Row Data in GridView on Button Click in ASP.Net

In this we explained how to Get Selected Row Data in GridView on Button Click in ASP.Net. On Select button click we fetch data of that particular row and display it on a Label control. In Earlier post we have learnt How to Store and Retrieve Images from SQL Server Database using Asp.NetHow to Create a directory using ASP.NET and C# How To Print GridView in Asp.Net and CRUD Operations Using Entity Framework in ASP.NET MVC.

On the OnSelectedIndexChanged event handler of the ASP.Net GridView, the username is extracted directly through the BoundField, while the uid and uemail is extracted via finding the Label control inside the TemplateField column. Then the extracted values are then displayed in a Label control.

HTML Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get Selected Row Data in GridView on Button Click in ASP.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Get Selected Row Data in GridView on Button Click in ASP.Net</h1>    
<asp:GridView ID="gv" runat="server" Width="100%" AutoGenerateColumns="false" OnSelectedIndexChanged="gv_SelectedIndexChanged">
<Columns>
<asp:ButtonField Text="Select" CommandName="Select"  />
<asp:BoundField DataField="username" HeaderText="UserName" />

<asp:TemplateField HeaderText="UID">
<ItemTemplate><asp:Label ID="uidn" runat="server" Text='<%#Eval("uid") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="User Email">
<ItemTemplate><asp:Label ID="uemaill" runat="server" Text='<%#Eval("uemail") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
<br /><br />
Selected Data is: <asp:Label ID="lbldata" runat="server" Text=""></asp:Label>
<br /><br />

All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>| Back to article:<a href="http://hightechnology.in/get-selected-row-data-in-gridview-on-button-click-in-asp-net">Get Selected Row Data in GridView on Button Click in ASP.Net</a> | Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>

</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    String strcon = ConfigurationManager.ConnectionStrings["connnn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        binddata();
    }
    public void binddata()
    {
        SqlConnection con = new SqlConnection(strcon);
        try
        {
            con.Open();
        }
        catch { }
        string str = "select top(5) uid,username,uemail from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
        gv.DataBind();
        con.Close();
    }
    protected void gv_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Accessing BoundField Column
        string uname = gv.SelectedRow.Cells[1].Text;

        string uid = (gv.SelectedRow.FindControl("uidn") as Label).Text;
        string uemail = (gv.SelectedRow.FindControl("uemaill") as Label).Text;

        lbldata.Text = "<b>User Name:</b> " + uname + " <b>User ID:</b> " + uid + "<b>User Email:</b>" +uemail;
    }
}

Download Code: