954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding a checkbox column dynamically

Hope someone can help with this....

I recently incorporated these two classes into my code which dynamically adds a checkbox column to my datagrid.

public class CheckBoxItem: ITemplate
{
///
/// The CheckBoxItem constructor
///
/// true if the item is to be in its editable state, false for the item to be disabled.
public CheckBoxItem(bool editable)
{
readOnly = (editable==true)?false:true;
}

///
/// Instantiates the CheckBox that we wish to represent in this column.
///
/// The container into which the control or controls are added.
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);
}

///
/// Our CheckChanged event
///
public event EventHandler CheckedChanged;

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

// sqlUpdateCommand1.Parameters["@Object_id"].Value
// = int.Parse(container.Cells[0].Text);
// sqlUpdateCommand1.Parameters["@Boolean"].Value = box.Checked;

// ...
//
// }


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

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

private string val;
public string Val
{
get
{
return val;
}
set
{
val = value;
}
}


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

///
/// 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.
///
public bool AutoPostBack
{
set
{
autoPostBack = value;
}
get
{
return autoPostBack;
}
}

///
/// Handler for the DataBinding event where we bind the data for a specific row
/// to the CheckBox.
///
/// The raiser of the event.
/// A System.EventArgs that contains the event data.
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.Columns[dataField].DataType;
if (data.Length>0)
{
switch (t.ToString())
{
case "System.Boolean":
if (( data == "True") || (data == "true"))
{
box.Checked = true;
}
break;
default:
break;
}
}
}

///
/// Internal storage for the readOnly flag.
///
private bool readOnly = true;
}

public class CheckBoxColumn:System.Web.UI.WebControls.TemplateColumn
{
///
/// Initialise our CheckBoxColumn.
///
public CheckBoxColumn()
{
// set the view one as readonly
viewItem = new CheckBoxItem(false); // SAW was false
this.ItemTemplate = viewItem as ITemplate;

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

///
/// Initialise our CheckBoxColumn with an optional ImmediatePostback capability.
///
/// If true then each change of state of the CheckBox item
/// will cause an event to be fired immediately on the server.
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;
}

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

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

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

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

///
/// Internal storage of the CheckBoxItem that is to be used for the edit state.
///
private CheckBoxItem editItem;
}


Before I this checkbox column/item code, I simply had 2 columns in my datagrid, one column was the "select" in which the client would click, and I used btnSelect_Clicked (able to create in my html-generated code) as my handler, and the other was an "active" column which changed everytime I clicked the respective dataGrid row (grid.SelectedIndex). Now since I changed my code to incorporate the checkbox column, I can't seem to extract the necessary "true/false" data. Maybe it is within the OncheckedChanged function?? I cannot for the life of me figure out how to retrieve some sort of variable to check if the box is checked (true) or unchecked (false). I see box.Checked... but that is not accessible when accessing the actual datagrid. Maybe it would be better for me if you can answer the question: can you specifically pull the checkbox data with a certain cell in the datagrid and use that certain variable to check if it is true/false?? I tried something like

if(dataTbl.Rows[grdResults.SelectedIndex]["Boolean"].ToString() == "true")
{currentCustomer.CustomerDiscounts[grdResults.SelectedIndex].ActiveFlag = true;
else
{currentCustomer.CustomerDiscounts[grdResults.SelectedIndex].ActiveFlag = false;

(I'm trying to set my ActiveFlag field in respect to the respective CheckBox field that is changed)

Thanks in advance!!

recinto
Newbie Poster
1 post since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

I would try not to use ToString() instead used:

dataTbl.Rows[grdResults.SelectedIndex]["Boolean"].Checked == true

More broken down it looks like:

DataRow dr = dataTbl.Rows[grdResults.SelectedIndex];
bool yesNo = dr["boolean"].Checked; // this assumes that "boolean" is the name of the column
if(yesNo)
doStuff();
else
doOtherSTuff();


Hope this helps, haven't tried it yet though.

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You