Solved

GridView CheckBox OnCheckChanged Save To DataTable?

Posted on 2012-03-19
8
397 Views
Last Modified: 2012-03-20
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;
       

}
0
Comment
Question by:pointeman
  • 4
  • 4
8 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37737686
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("Status",typeof(bool));
DataRow dr=null;
foreach(GridViewRow gvr in GridView1.Rows)
{
      CheckBox cb = (CheckBox)gvr.FindControl("checkbox1");
      if(cb!=null)
      {
      dr=dt.NewRow();
      bool value = cb.Checked;
      dr[0]=value;
      dt.Rows.Add(dr);
      }
}
dt.AcceptChanges();

Open in new window

0
 

Author Comment

by:pointeman
ID: 37740130
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
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37740794
bind the id with gridviewrow using label then search for this label and add its value to another column in datatable..
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();

Open in new window


Table Will Have:-
Status    ID
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:pointeman
ID: 37741891
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.HeaderRow.FindControl("ckbxheader");

            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.ToString();
                dr[1] = ckbx.Checked.ToString();
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37742102
Do This:-

for(int i=0;i<GridView1.Rows.Count;i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox ckbx = (CheckBox)row.FindControl("checkbox1");

                if (cbSelectedHeader.Checked == true)
                    ckbx.Checked = true;
                else
                    ckbx.Checked = false;

                dr = dt.NewRow();
                dr[0] = GridView1.DataKeys[i].Value.ToString();
                dr[1] = ckbx.Checked.ToString();
                dt.Rows.Add(dr);
            }

Open in new window


Also read here:-
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
0
 

Author Comment

by:pointeman
ID: 37743853
The problem is with the datatable add row because the datakeys return nicely like so:

foreach (GridViewRow row in GridView1.Rows)
{
     System.Web.HttpContext.Current.Response.Write(GridView1.DataKeys[row.RowIndex]["col_id"].ToString() + "<br/>");
}
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37743949
glad you got the answer, please close the question....
0
 

Author Closing Comment

by:pointeman
ID: 37744486
thx a million
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now