Hi ahmadsubuhanlubis,
Refer below code
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Name">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Get" runat="server" OnClick="GetDetails" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
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 GetDetails(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkBox = (row.FindControl("CheckBox1") as CheckBox);
if (checkBox.Checked)
{
string name = GridView1.DataKeys[row.RowIndex].Values[0].ToString();
string country = (row.FindControl("txtCountry") as TextBox).Text;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert" + row.RowIndex,
"alert('" + string.Format("Name:{0}\\nCountry:{1}", name, country) + "');", true);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
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 If
End Sub
Protected Sub GetDetails(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim checkBox As CheckBox = (TryCast(row.FindControl("CheckBox1"), CheckBox))
If checkBox.Checked Then
Dim name As String = GridView1.DataKeys(row.RowIndex).Values(0).ToString()
Dim country As String = (TryCast(row.FindControl("txtCountry"), TextBox)).Text
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert" & row.RowIndex, "alert('" & String.Format("Name:{0}\nCountry:{1}", name, country) & "');", True)
End If
Next
End Sub
Screenshot