Home > Forum > ASP.NET > Gridview Rows Count Zero on ButtonClick Event

Gridview Rows Count Zero on ButtonClick Event

by Thamil   on Aug 05, 2013   Category: ASP.NET  |  Views: 3423    |  Points: 5   |  Gold 
  Reply
Hi All,

I Have Issue with Gridview Rowcount on button click event

Button_Click Event
foreach(GridViewRow row in _gridView.Rows)
{
}

row count has zero after clicking button..

Question posted on behalf of Kumar R

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Gridview Rows Count Zero on ButtonClick Event   
by Thamil
on Aug 5 2013 2:57AM
Points : 10
Gold 
Hello Kumar,

Please see the code will help you how to get gridview row count in button click event and work with each row in gridview control.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Emp_Name");
dt.Columns.Add("Emp_id");
dt.Columns.Add("Emp_job");
dt.Columns.Add("Emp_Dep");
DataRow rw3 = dt.NewRow();
rw3["Emp_Name"] = "Muthu kumar";
rw3["Emp_id"] = "2700";
rw3["Emp_job"] = "Support Enginner";
rw3["Emp_Dep"] = "IT";
dt.Rows.Add(rw3);
DataRow rw4 = dt.NewRow();
rw4["Emp_Name"] = "Arvind";
rw4["Emp_id"] = "4678";
rw4["Emp_job"] = "Sr Software Engineer";
rw4["Emp_Dep"] = "IT";
dt.Rows.Add(rw4);
DataRow rw5 = dt.NewRow();
rw5["Emp_Name"] = "Raja ram";
rw5["Emp_id"] = "2300";
rw5["Emp_job"] = "Test Engineer";
rw5["Emp_Dep"] = "IT";
dt.Rows.Add(rw5);
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
int GVRowsCount = GridView1.Rows.Count;
foreach (GridViewRow row in GridView1.Rows)
{
string strName = row.Cells[0].Text.ToString();
string strId = row.Cells[1].Text.ToString();
string strJob = row.Cells[2].Text.ToString();
string strDep = row.Cells[3].Text.ToString();
}

}

Recent Post

Latest Posts