Hi ramco1917,
Please refer below sample.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label Text='<%#Eval("Country") %>' runat="server" />
<asp:HiddenField ID="hfId" runat="server" Value='<%#Eval("CustomerId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Save" runat="server" OnClick="Save" />
<hr />
<asp:GridView runat="server" ID="gvIds"></asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
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("CustomerId", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string))});
dt.Rows.Add("01", "John Hammond", "United States");
dt.Rows.Add("02", "Mudassar Khan", "India");
dt.Rows.Add("03", "Suzanne Mathews", "France");
dt.Rows.Add("04", "Robert Schidner", "Russia");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
List<string> ids = new List<string>();
foreach (GridViewRow row in gvDetails.Rows)
{
ids.Add((row.FindControl("hfId") as HiddenField).Value);
}
gvIds.DataSource = ids;
gvIds.DataBind();
}
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("CustomerId", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add("01", "John Hammond", "United States")
dt.Rows.Add("02", "Mudassar Khan", "India")
dt.Rows.Add("03", "Suzanne Mathews", "France")
dt.Rows.Add("04", "Robert Schidner", "Russia")
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim ids As List(Of String) = New List(Of String)()
For Each row As GridViewRow In gvDetails.Rows
ids.Add((TryCast(row.FindControl("hfId"), HiddenField)).Value)
Next
gvIds.DataSource = ids
gvIds.DataBind()
End Sub
Screenshot