Hi @atul,
Try the following sample.
Html Code
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:HiddenField ID="hfCustomerId" runat="server" Value='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Customer Name" />
</Columns>
</asp:GridView>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.gvCustomers.DataSource = this.GetData();
this.gvCustomers.DataBind();
this.AccessGridData();
}
}
private void AccessGridData()
{
int customerId = 0;
string customerName = string.Empty;
foreach (GridViewRow row in this.gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
customerId = int.Parse((row.FindControl("hfCustomerId") as HiddenField).Value);
customerName = row.Cells[1].Text;
}
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Name") });
dt.Rows.Add(1, "Ashutosh");
dt.Rows.Add(2, "Ram Krishna");
dt.Rows.Add(3, "Saajid");
dt.Rows.Add(4, "Shailesh");
return dt;
}
Namespaces:
using System;
using System.Web.UI.WebControls;
using System.Data;