473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Adding a Check Box Column to a DataGrid - Cannot get example to work (ASP.NET & C#)

I tried to use the following example, to add a checkbox column to a
DataGrid in an ASP.NET application:
http://www.codeproject.com/aspnet/datagridcheckbox.asp

For some reason, I simply CAN'T get the example to work. I created the
following two classes, provided with the example:

*-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn
Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DataGridTest2
{
/// <summary>
/// Summary description for CheckBoxColumn.
/// </summary>
public class CheckBoxColumn
:System.Web.UI.WebControls.TemplateColumn
{
/// <summary>
/// Initialise our CheckBoxColumn.
/// </summary>
public CheckBoxColumn()
{
// set the view one as readonly
//viewItem = new CheckBoxItem(false); // SAW was false
viewItem = new CheckBoxItem(true);
this.ItemTemplate = viewItem as ITemplate;

// let the edit check box be editable
editItem = new CheckBoxItem(true);
this.EditItemTemplate = editItem as ITemplate;
}

/// <summary>
/// Initialise our CheckBoxColumn with an optional
ImmediatePostback capability.
/// </summary>
/// <param name="ImmediatePostback">If true then each change
of state of the
/// CheckBox item
/// will cause an event to be fired immediately on the
server.</param>
public CheckBoxColumn(bool ImmediatePostback)
{
// set the view one as readonly
viewItem = new CheckBoxItem(ImmediatePostback);
this.ItemTemplate = viewItem as ITemplate;

// let the edit check box be editable
editItem = new CheckBoxItem(true);
this.EditItemTemplate = editItem as ITemplate;

AutoPostBack = ImmediatePostback;
}

/// <summary>
/// Occurs when the value of the Checked property changes
between posts to the
/// server.
/// </summary>
/// <remarks>
/// The <b>CheckedChanged</b> event is raised when the value
of the Checked
/// property changes
/// between posts to the server.
/// <b>Note</b> This event does not post the page back to
the server unless the
/// AutoPostBack property is set to true.
/// <b>Note</b> The control must have viewstate enabled for
the
/// <b>CheckedChanged</b> event to work correctly.
/// </remarks>
public event EventHandler CheckedChanged
{
add
{
viewItem.CheckedChanged += value;
editItem.CheckedChanged += value;
}
remove
{
viewItem.CheckedChanged -= value;
editItem.CheckedChanged -= value;
}
}

/// <summary>
/// If true then then each click on a CheckBox will cause an
event to be fired on the
/// server.
/// </summary>
public bool AutoPostBack
{
set
{
viewItem.AutoPostBack = value;
editItem.AutoPostBack = value;
}
get
{
return viewItem.AutoPostBack;
}
}

/// <summary>
/// The DataField that we wish our control to bind to.
/// </summary>
public string DataField
{
get
{
return viewItem.DataField;
}
set
{
viewItem.DataField = value;
editItem.DataField = value;
}
}

/// <summary>
/// Internal storage of the CheckBoxItem that is to be used
for the view state.
/// </summary>
private CheckBoxItem viewItem;

/// <summary>
/// Internal storage of the CheckBoxItem that is to be used for
the edit state.
/// </summary>
private CheckBoxItem editItem;
}
}
*-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxItem
Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DataGridTest2
{
/// <summary>
///
/// </summary>
internal class CheckBoxItem : ITemplate
{
/// <summary>
/// The CheckBoxItem constructor
/// </summary>
/// <param name="editable">true if the item is to be in its
editable state, false for the
/// item to be
/// disabled.</param>
public CheckBoxItem(bool editable)
{
readOnly = (editable==true)?false:true;
}

/// <summary>
/// Instantiates the CheckBox that we wish to represent in
this column.
/// </summary>
/// <param name="container">The container into which the
control or controls are
/// added.</param>
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.DataBinding += new EventHandler(this.BindData);
box.AutoPostBack = autoPostBack;
box.CheckedChanged += new
EventHandler(this.OnCheckChanged);
container.Controls.Add(box);
}

/// <summary>
/// Our CheckChanged event
/// </summary>
public event EventHandler CheckedChanged;

/// <summary>
/// This is a common handler for all the Checkboxes.
/// </summary>
/// <param name="sender">The raiser of this event a
CheckBox.</param>
/// <param name="e">A System.EventArgs that contains the
event data.</param>
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}

/// <summary>
/// The internal storage for which DataField we are going to
represent.
/// </summary>
private string dataField;

/// <summary>
/// Used to set the DataField that we wish to represent with
this CheckBox.
/// </summary>
public string DataField
{
get
{
return dataField;
}
set
{
dataField=value;
}
}

/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=false;

/// <summary>
/// Set the AutoPostBack flag. If this is true then each time
a CheckBox is clicked
/// in the Column that contains this item then an event is
raised on the server.
/// </summary>
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}

/// <summary>
/// Handler for the DataBinding event where we bind the data
for a specific row
/// to the CheckBox.
/// </summary>
/// <param name="sender">The raiser of the event.</param>
/// <param name="e">A System.EventArgs that contains the
event data.</param>
private void BindData(object sender, EventArgs e)
{
CheckBox box = (CheckBox) sender;
DataGridItem container = (DataGridItem)
box.NamingContainer;
box.Checked = false;
box.Enabled = (readOnly == true) ? false:true;
string data = ((DataRowView)
container.DataItem)[dataField].ToString();
Type t =
((DataRowView)container.DataItem).DataView.Table.C olumns
[dataField].DataType;
if (data.Length>0)
{
switch (t.ToString())
{
case "System.Boolean":
if (( data == "True") || (data == "true"))
{
box.Checked = true;
}
break;
default:
break;
}
}
}
/// <summary>
/// Internal storage for the readOnly flag.
/// </summary>
private bool readOnly = true;
}
}
*-*-*-*-*-*-*-*-*-*-*-*-*-*Custom Control Code Behind
Page:-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*

namespace DataGridTest2
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for OrderPickerControl.
/// </summary>
public class OrderPickerControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
cn= new SqlConnection
("Server=localhost;uid=sa;pwd=pwd;database=testDB" );
da= new SqlDataAdapter ("SELECT CustID, IsComplete FROM
Orders ", cn);
ds= new DataSet ();
da.Fill (ds, "Orders");
DataGrid1.DataSource =ds.Tables[0];

// Add the new column to the DataGrid
CheckBoxColumn chkColumn = new CheckBoxColumn();

chkColumn.HeaderText = "Include?";
chkColumn.DataField = "IsComplete";
//chkColumn.AutoPostBack = true;
//chkColumn.CheckedChanged +=new EventHandler(this.o);
DataGrid1.Columns.Add(chkColumn);
DataGrid1.DataBind ();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
The DataGrid is bound properly, and shows the correct state of the
check boxes for the underlying SQL bit datatype field (IsComplete).
What I can't figure out is how to immediately update the database, once
a checkbox has been checked or unchecked. I was unable to determine how
to do this, based on the example. The IsComplete field contains either
0 values for false, or 1 values for true. Checking the checkbox should
change the 0 values to 1 values (and unchecking should change 1 values
to 0 values). I also want to be able to refresh the DataGrid to show
these changes. (I thought that AutoPostBack would accomplish this, but
I had no luck getting it to work.)

The worst part of it is I know that I'm probably missing something
obvious, due to my lack of experience. Needless to say, any help,
advice, snippets, etc. would be greatly appreciated.

THANKS!!!

Nov 19 '05 #1
1 4198
sianan,

I have code that shows how to find out which checkbox in the datagrid has
been checked on post back. The sample code is in vb.net, but I think you'll
be able to see what it's doing as most of it is using built in objects whos
code will look much the same as c#. If you have any trouble telling what a
routine does I'd be happy to convert that line(s) of code to c#. Just email
me with any questions.

You may find the sample code at:
http://www.aboutfortunate.com?page=codelibrary

Use the search box there to search for: "checkbox in datagrid"

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"sianan" <sm*********@tds.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I tried to use the following example, to add a checkbox column to a
DataGrid in an ASP.NET application:
http://www.codeproject.com/aspnet/datagridcheckbox.asp

For some reason, I simply CAN'T get the example to work. I created the
following two classes, provided with the example:

*-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn
Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DataGridTest2
{
/// <summary>
/// Summary description for CheckBoxColumn.
/// </summary>
public class CheckBoxColumn
:System.Web.UI.WebControls.TemplateColumn
{
/// <summary>
/// Initialise our CheckBoxColumn.
/// </summary>
public CheckBoxColumn()
{
// set the view one as readonly
//viewItem = new CheckBoxItem(false); // SAW was false
viewItem = new CheckBoxItem(true);
this.ItemTemplate = viewItem as ITemplate;

// let the edit check box be editable
editItem = new CheckBoxItem(true);
this.EditItemTemplate = editItem as ITemplate;
}

/// <summary>
/// Initialise our CheckBoxColumn with an optional
ImmediatePostback capability.
/// </summary>
/// <param name="ImmediatePostback">If true then each change
of state of the
/// CheckBox item
/// will cause an event to be fired immediately on the
server.</param>
public CheckBoxColumn(bool ImmediatePostback)
{
// set the view one as readonly
viewItem = new CheckBoxItem(ImmediatePostback);
this.ItemTemplate = viewItem as ITemplate;

// let the edit check box be editable
editItem = new CheckBoxItem(true);
this.EditItemTemplate = editItem as ITemplate;

AutoPostBack = ImmediatePostback;
}

/// <summary>
/// Occurs when the value of the Checked property changes
between posts to the
/// server.
/// </summary>
/// <remarks>
/// The <b>CheckedChanged</b> event is raised when the value
of the Checked
/// property changes
/// between posts to the server.
/// <b>Note</b> This event does not post the page back to
the server unless the
/// AutoPostBack property is set to true.
/// <b>Note</b> The control must have viewstate enabled for
the
/// <b>CheckedChanged</b> event to work correctly.
/// </remarks>
public event EventHandler CheckedChanged
{
add
{
viewItem.CheckedChanged += value;
editItem.CheckedChanged += value;
}
remove
{
viewItem.CheckedChanged -= value;
editItem.CheckedChanged -= value;
}
}

/// <summary>
/// If true then then each click on a CheckBox will cause an
event to be fired on the
/// server.
/// </summary>
public bool AutoPostBack
{
set
{
viewItem.AutoPostBack = value;
editItem.AutoPostBack = value;
}
get
{
return viewItem.AutoPostBack;
}
}

/// <summary>
/// The DataField that we wish our control to bind to.
/// </summary>
public string DataField
{
get
{
return viewItem.DataField;
}
set
{
viewItem.DataField = value;
editItem.DataField = value;
}
}

/// <summary>
/// Internal storage of the CheckBoxItem that is to be used
for the view state.
/// </summary>
private CheckBoxItem viewItem;

/// <summary>
/// Internal storage of the CheckBoxItem that is to be used for
the edit state.
/// </summary>
private CheckBoxItem editItem;
}
}
*-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxItem
Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DataGridTest2
{
/// <summary>
///
/// </summary>
internal class CheckBoxItem : ITemplate
{
/// <summary>
/// The CheckBoxItem constructor
/// </summary>
/// <param name="editable">true if the item is to be in its
editable state, false for the
/// item to be
/// disabled.</param>
public CheckBoxItem(bool editable)
{
readOnly = (editable==true)?false:true;
}

/// <summary>
/// Instantiates the CheckBox that we wish to represent in
this column.
/// </summary>
/// <param name="container">The container into which the
control or controls are
/// added.</param>
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.DataBinding += new EventHandler(this.BindData);
box.AutoPostBack = autoPostBack;
box.CheckedChanged += new
EventHandler(this.OnCheckChanged);
container.Controls.Add(box);
}

/// <summary>
/// Our CheckChanged event
/// </summary>
public event EventHandler CheckedChanged;

/// <summary>
/// This is a common handler for all the Checkboxes.
/// </summary>
/// <param name="sender">The raiser of this event a
CheckBox.</param>
/// <param name="e">A System.EventArgs that contains the
event data.</param>
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}

/// <summary>
/// The internal storage for which DataField we are going to
represent.
/// </summary>
private string dataField;

/// <summary>
/// Used to set the DataField that we wish to represent with
this CheckBox.
/// </summary>
public string DataField
{
get
{
return dataField;
}
set
{
dataField=value;
}
}

/// <summary>
/// The internal storage for the AutoPostback flag.
/// </summary>
private bool autoPostBack=false;

/// <summary>
/// Set the AutoPostBack flag. If this is true then each time
a CheckBox is clicked
/// in the Column that contains this item then an event is
raised on the server.
/// </summary>
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}

/// <summary>
/// Handler for the DataBinding event where we bind the data
for a specific row
/// to the CheckBox.
/// </summary>
/// <param name="sender">The raiser of the event.</param>
/// <param name="e">A System.EventArgs that contains the
event data.</param>
private void BindData(object sender, EventArgs e)
{
CheckBox box = (CheckBox) sender;
DataGridItem container = (DataGridItem)
box.NamingContainer;
box.Checked = false;
box.Enabled = (readOnly == true) ? false:true;
string data = ((DataRowView)
container.DataItem)[dataField].ToString();
Type t =
((DataRowView)container.DataItem).DataView.Table.C olumns
[dataField].DataType;
if (data.Length>0)
{
switch (t.ToString())
{
case "System.Boolean":
if (( data == "True") || (data == "true"))
{
box.Checked = true;
}
break;
default:
break;
}
}
}
/// <summary>
/// Internal storage for the readOnly flag.
/// </summary>
private bool readOnly = true;
}
}
*-*-*-*-*-*-*-*-*-*-*-*-*-*Custom Control Code Behind
Page:-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*

namespace DataGridTest2
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for OrderPickerControl.
/// </summary>
public class OrderPickerControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
cn= new SqlConnection
("Server=localhost;uid=sa;pwd=pwd;database=testDB" );
da= new SqlDataAdapter ("SELECT CustID, IsComplete FROM
Orders ", cn);
ds= new DataSet ();
da.Fill (ds, "Orders");
DataGrid1.DataSource =ds.Tables[0];

// Add the new column to the DataGrid
CheckBoxColumn chkColumn = new CheckBoxColumn();

chkColumn.HeaderText = "Include?";
chkColumn.DataField = "IsComplete";
//chkColumn.AutoPostBack = true;
//chkColumn.CheckedChanged +=new EventHandler(this.o);
DataGrid1.Columns.Add(chkColumn);
DataGrid1.DataBind ();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
The DataGrid is bound properly, and shows the correct state of the
check boxes for the underlying SQL bit datatype field (IsComplete).
What I can't figure out is how to immediately update the database, once
a checkbox has been checked or unchecked. I was unable to determine how
to do this, based on the example. The IsComplete field contains either
0 values for false, or 1 values for true. Checking the checkbox should
change the 0 values to 1 values (and unchecking should change 1 values
to 0 values). I also want to be able to refresh the DataGrid to show
these changes. (I thought that AutoPostBack would accomplish this, but
I had no luck getting it to work.)

The worst part of it is I know that I'm probably missing something
obvious, due to my lack of experience. Needless to say, any help,
advice, snippets, etc. would be greatly appreciated.

THANKS!!!

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
6
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
1
by: VB Programmer | last post by:
I have a datagrid which is bound to a datatable that I create on the fly. How do I set the width of each column via code? Ex: Column 1 is always 100, Column 2 is always 250, etc... Thanks!
4
by: Craig G | last post by:
i was using the following on a serverside button on a form If (Not Page.IsPostBack) Then Me.BtnDelete.Attributes.Add("onclick","return confirm('Are you sure you want to delete?');") End If ...
1
by: RN | last post by:
Hi. I used this article to add a template column to a datagrid that is entirely created with code: ...
9
by: Steve | last post by:
How I can remove an AutoGenerated column? I wnat to inlcude the primary key in the resultset for creating some custom LinkButtons, but I don't want it (the PK) displayed in the DataGrid. I tried...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.