pointeman
asked on
GridView CheckBox OnCheckChanged Save To DataTable?
I need to save ALL GridView CheckBoxes values to a dataset/datatable. This gets more interesting using a DataKey for col_ID. I have this much code:
foreach(GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl( "checkbox1 ");
bool value = cb.Checked;
}
foreach(GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl(
bool value = cb.Checked;
}
ASKER
Need to add 2 items in the same row very simply. The Gridview datakey is col_ID.
row = "ID" "Value"
The need a foreach to check the data. thx
row = "ID" "Value"
The need a foreach to check the data. thx
bind the id with gridviewrow using label then search for this label and add its value to another column in datatable..
Table Will Have:-
Status ID
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("Status",typeof(bool));
dt.Columns.Add(new DataColumn("ID",typeof(string));
DataRow dr=null;
foreach(GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl("checkbox1");
Label lb = (Label)gvr.FindControl("lblId");
if(cb!=null && lb!=null)
{
dr=dt.NewRow();
bool value = cb.Checked;
dr[0]=value;
dr[1]=lb.Text;
dt.Rows.Add(dr);
}
}
dt.AcceptChanges();
Table Will Have:-
Status ID
ASKER
Geting incorrect results like below, although the col_active is correct, the col_id should read 1-9
col_id= 9 col_active= True
col_id= 9 col_active= True
col_id= 9 col_active= True
col_id= 9 col_active = True
col_id= 9 col_active = True
[code so far]
DataTable dt = new DataTable();
dt.Columns.Add("col_id");
dt.Columns.Add("col_active ");
DataRow dr = null;
CheckBox cbSelectedHeader = (CheckBox)GridView1.Header Row.FindCo ntrol("ckb xheader");
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ckbx = (CheckBox)row.FindControl( "checkbox1 ");
if (cbSelectedHeader.Checked == true)
ckbx.Checked = true;
else
ckbx.Checked = false;
dr = dt.NewRow();
dr[0] = GridView1.DataKeys[row.RowIndex].Value.ToStr ing();
dr[1] = ckbx.Checked.ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
col_id= 9 col_active= True
col_id= 9 col_active= True
col_id= 9 col_active= True
col_id= 9 col_active = True
col_id= 9 col_active = True
[code so far]
DataTable dt = new DataTable();
dt.Columns.Add("col_id");
dt.Columns.Add("col_active
DataRow dr = null;
CheckBox cbSelectedHeader = (CheckBox)GridView1.Header
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox ckbx = (CheckBox)row.FindControl(
if (cbSelectedHeader.Checked == true)
ckbx.Checked = true;
else
ckbx.Checked = false;
dr = dt.NewRow();
dr[0] = GridView1.DataKeys[row.RowIndex].Value.ToStr
dr[1] = ckbx.Checked.ToString();
dt.Rows.Add(dr);
}
dt.AcceptChanges();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem is with the datatable add row because the datakeys return nicely like so:
foreach (GridViewRow row in GridView1.Rows)
{
System.Web.HttpContext.Cur rent.Respo nse.Write( GridView1. DataKeys[r ow.RowInde x]["col_id "].ToStrin g() + "<br/>");
}
foreach (GridViewRow row in GridView1.Rows)
{
System.Web.HttpContext.Cur
}
glad you got the answer, please close the question....
ASKER
thx a million
Open in new window