I am trying to add checked rows's ID in Gridview to a data table ( memory) …so I can use it for other process..
But it only add the ID from the last checked row in gridview…( for excample, I checked three rows , it only add the last checked rows to a data table) what am I doing wrong? How can I fix this?
protected void LinkButton_AddNewUser_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
DataRow dataRow;
dataRow = dt.NewRow();
for (int i = 0; i <= GV_NewUser.Rows.Count - 1; i++)
foreach (GridViewRow di in GV_NewUser.Rows)
{
CheckBox chk = (CheckBox)di.FindControl("AddUsers");
if (chk != null && chk.Checked)
{
dataRow["ID"] = di.Cells[0].Text;
dt.Rows.Add(dataRow);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
Koteswararao MallisettiPosted Oct 29, 2010, 1:02 AM
if (chk != null && chk.Checked)
{
dataRow["ID"] = di.Cells[0].Text;
dt.Rows.Add(dataRow);
means the chek box is checked only the row is added to datatable at the same time we will take new datarow in the foreach loop otherwise it always displays only one row at any time