Hi ramco1917,
Refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" OnRowCommand="OnRowCommand">
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
// Adding CommandField column.
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Link;
cf.ShowSelectButton = true;
gvCustomers.Columns.Add(cf);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
}
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Select")
{
string name = gvCustomers.Rows[index].Cells[2].Text;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Selected Name: " + name + "');", true);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
' Adding CommandField column.
Dim cf As CommandField = New CommandField()
cf.ButtonType = ButtonType.Link
cf.ShowSelectButton = True
gvCustomers.Columns.Add(cf)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Protected Sub OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "Select" Then
Dim name As String = gvCustomers.Rows(index).Cells(2).Text
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('Selected Name: " & name & "');", True)
End If
End Sub
Screenshot