Midnight Development Blog

February 27, 2008

Implementing Data Grid CheckBox Column Step by Step

Filed under: ASP.NET, ASP.NET 1.1 — antigenius @ 12:45 pm

Introduction

Purpose
of this article is to demonstrate architecture of data grid TemplateColumn class using an example
implementation of data grid custom selection column. So, one should be able to
design more complex custom columns easily.After reading this you will be able to implement DataGrid Progress column and other custom columns

Abstract

Let
us examine the data grid Template Column first, you will find the following definition
of Data grid TemplateColumn class in dot net framework.

public class TemplateColumn : DataGridColumn
{
public TemplateColumn();
public virtual ITemplate EditItemTemplate { get; set; }
public virtual ITemplate FooterTemplate { get; set; }
public virtual ITemplate HeaderTemplate { get; set; }
public virtual ITemplate ItemTemplate { get; set; }
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType);
}

As the difination sugesst we can set these properties to any
class that implements ITemplate interface.

 

To implement a selection column in data grid we need
to add Template column That will display a check
box in its and header template and also a check box for each of item template
like the above figure

Step 1:Adding Required Classes

Add a class in your project
named “CheckBoxColumn
and extend it from System.Web.UI.WebControls.TemplateColumn
class.your class defination should look like

public class CheckBoxColumn:TemplateColumn
{
.. ... ...
}

Add two more classes in your project
named “CheckBoxFieldTemplate” and “CheckBoxHeaderTemplate” both should
implement ITemplate interface,their definitation should
look like this

class CheckBoxFieldTemplate:ITemplate
{
public void InstantiateIn(Control container)
{
}
}

class CheckBoxHeaderTemplate:ITemplate
{
public void InstantiateIn(Control container)
{
}
}

 

ITemplate interface contains only a single
function

void InstantiateIn(Control container); when implemneted by a class, defines the System.Web.UI.Control object, that child
controls and templates belong to. These child controls are in turn defined
within an inline template

 

Step 2:Adding CheckBox and Attaching Javascript

 

We need to
display checkbox in both header and item Template so in both classes our InstantiateIn method should look like
this

 

public void InstantiateIn(Control container){

DataGridItem container =(DataGridItem)container;

CheckBox chkSelect = new CheckBox();

chkSelect.DataBinding += new EventHandler(this.OnDataBinding);

chkSelect.Attributes.Add(onClick”, return HighLightRow(this,” + container.ItemIndex.ToString() + ,’” + this._selectColor.Name + ‘,’” + this._unSelectColor.Name + ‘);”);

container.Controls.Add(chkSelect);
}

 

Step 3:Assigning ItemTemplates To Custom Column

We have our
required templates that can be used with our custom column.

Now you
have to set two inherited properties of our custom column ItemTemplate and
HeaderTemplate.

As the difination sugesst you can set
these properties to any class that implements ITemplate interface.our
classes created in step one fullfill that reuiqrement. So, custom Template
column class should look like this.

 

 

public class CheckBoxColumn:TemplateColumn
{
private CheckBoxHeaderTemplate CheckBoxHeaderTemplate = new CheckBoxHeaderTemplate();
private CheckBoxFieldTemplate CheckBoxFieldTemplate = new CheckBoxFieldTemplate();
public CheckBoxColumn():base()
{
this.ItemTemplate = CheckBoxFieldTemplate;
this.HeaderTemplate = CheckBoxHeaderTemplate;
}
}

you done with it now you can
add this columns of data grid by reference its assembly in you page add
appropriate JavaScript for Header CheckBox and for ItemCheckBox.

 

Step 4:Adding Databinding Functionality

Finally we need to implement databinding functionality so our
custom column will also work as a databound column if required.

Expose a property in you CheckBoxColumn
Class named “DataField”

Modify the consuctor of CheckBoxFieldTemplate to accept the
column name to bind with. Now add override databinding event of ITemplate by placing the following code

 

 

public void OnDataBinding(object sender, EventArgs e)
{
if (this._columnName.Length > 0)
{
CheckBox chk = (CheckBox)sender;
DataGridItem container =
(DataGridItem)chk.NamingContainer;
chk.Checked = Convert.ToBoolean(((DataRowView)container.DataItem)[this._columnName]);
}
}

 

 

 

 

Step 5:Download Demo and Attach Javascript

Download Jscript2.js attached in demo project and add into your project directory and
place its reference on the top of your page.

using The code

 

you are able to add a check box column with check All , uncheck All and highlight selected rows and also have databinding functionality by just adding this custom checkbox column in your grid as follows.

<CC1:CheckBoxColumn> </CC1:CheckBoxColumn>

Points of Interest

You are able to make custom columns like progress column, fixed length column that display …. after a specified length of string and can reuse these over all your application.

November 20, 2007

Parital Page Validation using validation controls

Filed under: ASP.NET — antigenius @ 5:44 am

Previously i had posted an article on www.codeporject.com that is specifically for asp.net 1.0 and 1.1 when validation controls are bound the to validate whole of the page.This article discuss some client side and server side tricks to partially validating a page.In Asp.net 2.0 and later Microsoft provided validation groups that provide same parial validation functionality that i have tried to achieve in ASP.net 1.0 and 1.1. You can have a look at that post partial Page Validation.

November 12, 2007

Date Control Better than DateTimePicker

Filed under: VB.NET — antigenius @ 5:20 am

In a database application Date data type handling is a bit difficult as compared to other data types and getting input by keyboard is required some extra efforts.Common solution to this problem is the DateTimePicker control provided by Microsoft.Unfortunately DateTimePicker is not best suited for fast data entry via keyboard that is always required in a database driven applications. I have got several complains from end users that he don’t want to use mouse for date selection or when entering date in DateTimePicker via keyboard require extra keys has to be pressed from moving cursor from one day part to next and so on. This is the motivation behind the development of a new DateTimePicker, which will enable users to enter data fast and in alternate ways either by keyboard or by mouse you can find my article and source for Date Control at www.codeproject.com.

Blog at WordPress.com.