In this article I will explain with an example, how to dynamically add LinkButton with RowCommand to GridView in ASP.Net using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follow.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
HTML Markup
The HTML Markup consists of following control:
GridView – For displaying data.
Columns
The GridView consists of three BoundField columns.
<asp:GridView ID="gvCustomers" runat="server" OnRowCommand="OnRowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
You will need to import following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Populating GridView and adding LinkButton column suing C# and VB.Net
Inside the Page_Load event handler, first the connection string is read from the Web.Config file and the SELECT query is defined.
Then, a connection to the database is established using the SqlConnection class.
The SqlDataAdapter object is initialized with the SqlCommand and using the Fill function, the DataTable is populated with the records from database.
Then, an object of CommandField class is created and the ButtonType and ShowSelectButton properties are set as Link and TRUE respectively.
After that, the CommandFiled is added as column to the GridView.
Finally, the DataTable is assigned to the GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sql = "SELECT CustomerId, Name, Country FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
// Adding CommandField column.
CommandField commandField = new CommandField();
commandField.ButtonType = ButtonType.Link;
commandField.ShowSelectButton = true;
gvCustomers.Columns.Add(commandField);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(sql, con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
'Adding CommandField column.
Dim commandField As CommandField = New CommandField()
commandField.ButtonType = ButtonType.Link
commandField.ShowSelectButton = True
gvCustomers.Columns.Add(commandField)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Fetching selected value using RowCommand event
When the Select Button is clicked, the index of the GridView Row is determined using the CommandArgument and it is used to select the Name of the Customers from the respective column of the GridView.
C#
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 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
Demo
Downloads