Please refer below sample:
HTML
<form id="form1" runat="server">
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" OnRowDataBound="OnRowDataBound">
</asp:GridView>
</form>
Namespace
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TemplateField tfieldCheckbox = new TemplateField();
tfieldCheckbox.HeaderText = "Select";
GridView1.Columns.Add(tfieldCheckbox);
BoundField bfield = new BoundField();
bfield.HeaderText = "Name";
bfield.DataField = "Name";
GridView1.Columns.Add(bfield);
TemplateField tfield = new TemplateField();
tfield.HeaderText = "Country";
GridView1.Columns.Add(tfield);
tfield = new TemplateField();
tfield.HeaderText = "View";
GridView1.Columns.Add(tfield);
this.GridView1.AutoGenerateColumns = false;
}
this.BindGrid();
}
protected void CheckedChaged(object sender, EventArgs e)
{
this.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "alert('Hi')", true);
this.BindGrid();
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
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();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = new CheckBox();
checkbox.ID = "chkChecked";
checkbox.CheckedChanged += CheckedChaged;
checkbox.AutoPostBack = true;
e.Row.Cells[0].Controls.Add(checkbox);
TextBox txtCountry = new TextBox();
txtCountry.ID = "txtCountry";
txtCountry.Text = (e.Row.DataItem as DataRowView).Row["Country"].ToString();
e.Row.Cells[2].Controls.Add(txtCountry);
LinkButton lnkView = new LinkButton();
lnkView.ID = "lnkView";
lnkView.Text = "View";
lnkView.Click += ViewDetails;
lnkView.CommandArgument = (e.Row.DataItem as DataRowView).Row["Id"].ToString();
e.Row.Cells[3].Controls.Add(lnkView);
}
}
protected void ViewDetails(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string name = row.Cells[0].Text;
string country = (row.FindControl("txtCountry") as TextBox).Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Id: " + id + " Name: " + name + " Country: " + country + "')", true);
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.GridView1.AutoGenerateColumns = False
If Not Me.IsPostBack Then
Dim tfieldCheckbox As New TemplateField()
tfieldCheckbox.HeaderText = "Select"
GridView1.Columns.Add(tfieldCheckbox)
Dim bfield As New BoundField()
bfield.HeaderText = "Name"
bfield.DataField = "Name"
GridView1.Columns.Add(bfield)
Dim tfield As New TemplateField()
tfield.HeaderText = "Country"
GridView1.Columns.Add(tfield)
tfield = New TemplateField()
tfield.HeaderText = "View"
GridView1.Columns.Add(tfield)
End If
Me.BindGrid()
End Sub
Protected Sub CheckedChaged(sender As Object, e As EventArgs)
Me.ClientScript.RegisterClientScriptBlock(GetType(String), "alert", "alert('Hi')", True)
End Sub
Private Sub BindGrid()
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
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 Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim checkbox As New CheckBox()
checkbox.ID = "chkChecked"
AddHandler checkbox.CheckedChanged, AddressOf CheckedChaged
checkbox.AutoPostBack = True
e.Row.Cells(0).Controls.Add(checkbox)
Dim txtCountry As New TextBox()
txtCountry.ID = "txtCountry"
txtCountry.Text = TryCast(e.Row.DataItem, DataRowView).Row("Country").ToString()
e.Row.Cells(2).Controls.Add(txtCountry)
Dim lnkView As New LinkButton()
lnkView.ID = "lnkView"
lnkView.Text = "View"
AddHandler lnkView.Click, AddressOf ViewDetails
lnkView.CommandArgument = TryCast(e.Row.DataItem, DataRowView).Row("Id").ToString()
e.Row.Cells(3).Controls.Add(lnkView)
End If
End Sub
Protected Sub ViewDetails(sender As Object, e As EventArgs)
Dim lnkView As LinkButton = TryCast(sender, LinkButton)
Dim row As GridViewRow = TryCast(lnkView.NamingContainer, GridViewRow)
Dim id As String = lnkView.CommandArgument
Dim name As String = row.Cells(0).Text
Dim country As String = TryCast(row.FindControl("txtCountry"), TextBox).Text
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", (Convert.ToString((Convert.ToString((Convert.ToString("alert('Id: ") & id) + " Name: ") & name) + " Country: ") & country) + "')", True)
End Sub