Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Implementing a Custom DataGrid Selection Column Step by Step

, 20 Feb 2008
The purpose of this article is to demonstrate the architecture of the DataGrid TemplateColumn class using an example implementation of a DataGrid custom selection column.
GridDemo.zip
GridDemo
bin
CodeWorks.dll
GridDemo.dll
Global.asax
GridDemo.csproj.webinfo
_vti_cnf
_vti_pvt
access.cnf
deptodoc.btr
doctodep.btr
service.cnf
service.lck
services.cnf
_vti_script
_vti_txt
SelectionColumnSource.zip
CodeWorks
CodeWorks.csproj.user
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

namespace CodeWorks
{
	/// <summary>
	/// Summary description for CheckBoxFieldTemplate.
	/// </summary>
	
		public class CheckBoxFieldTemplate : ITemplate
		{
       
			
			private Color _selectColor;
			private Color _unSelectColor;
			private string _columnName = "";
			private int _rowReference = 0;
			public CheckBoxFieldTemplate(Color selectColor, Color unSelectColor)
			{
				this._selectColor = selectColor;
				this._unSelectColor = unSelectColor;
			}
			public CheckBoxFieldTemplate(string columnName,Color selectColor, Color unSelectColor)
			{
				this._selectColor = selectColor;
				this._unSelectColor = unSelectColor;
				this._columnName = columnName;
			}
			public void InstantiateIn(Control container)
			{
				_rowReference += 1;
				CheckBox chkSelect = new CheckBox();
				chkSelect.DataBinding += new EventHandler(this.OnDataBinding);
				chkSelect.Attributes.Add("onClick", "return HighLightRow(this," + _rowReference.ToString()+ ",'" + this._selectColor.Name + "','" + this._unSelectColor.Name + "');");
				
				chkSelect.EnableViewState=true;
				container.Controls.Add(chkSelect);
			}
			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]);
                
				}
			}
		}
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Muhammad Abubakar Dar
Software Developer (Senior) Systems Limited
Pakistan Pakistan
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 20 Feb 2008
Article Copyright 2008 by Muhammad Abubakar Dar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid