Click here to Skip to main content
15,895,746 members
Articles / Web Development / ASP.NET

ASP.NET Expand/Contract DataGrid

Rate me:
Please Sign up or sign in to vote.
4.38/5 (41 votes)
14 Apr 20041 min read 217.3K   4.7K   103  
ASP.NET Expand/Contract DataGrid that allows you to show/hide details without posting back.
// ---------------------------------------------------------
// Tony Truong's ExDataGrid
// Copyright (C) 2004 
// For further reference see http://www.codeproject.com
// ---------------------------------------------------------
// Much of the code below was written using elements from 
// Denis Baurer's HierarGrid server control
// ---------------------------------------------------------

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Resources;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Gorth.ExDataGrids
{
	/// <summary>
	/// The ExColumn is derived from the BoundColumn and simply add's id's to each row entry of the column
	/// </summary>
	public class ExColumn : System.Web.UI.WebControls.BoundColumn 
	{
		#region Property: ExGroupName
		private string _exGroupName;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public string ExGroupName
		{
			get { return _exGroupName; }
			set { _exGroupName = value; }
		}
		#endregion Property: ExGroupName

		#region Property: DrawBorders
		private bool _drawBorders;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public bool DrawBorders
		{
			get { return _drawBorders; }
			set { _drawBorders = value; }
		}
		#endregion Property: DrawBorders

		#region Property: BackColor
		private string _backColor;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public string BackColor
		{
			get { return _backColor; }
			set { _backColor = value; }
		}
		#endregion Property: BackColor

		#region Property: ForeColor
		private string _foreColor;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public string ForeColor
		{
			get { return _foreColor; }
			set { _foreColor = value; }
		}
		#endregion Property: ForeColor

		#region Property: Width
		private int _width;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public int Width
		{
			get { return _width; }
			set { _width = value; }
		}
		#endregion Property: Width

		/// <summary>
		/// Initializes a new instance of HierarColumn class.
		/// </summary>
		public ExColumn() : base()
		{
			_drawBorders = false;
			_backColor = "none";
			_foreColor = "none";
			_width = -1;
		}

		/// <summary>
		/// On initialization the ExColumn hide the columns and set the id/class names
		/// 
		/// </summary>
		/// <param name="cell"></param>
		/// <param name="columnIndex"></param>
		/// <param name="itemType"></param>
		public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
		{
			base.InitializeCell(cell, columnIndex, itemType);
			switch(itemType)
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
					cell.CssClass=_exGroupName;
					if(_drawBorders)
					{
						cell.Attributes.Add("style", "display:none");
					}
					else
					{
						cell.Attributes.Add("style", "display:none;border-style=none");
					}

					//add backcolor
					if(BackColor != "none")
					{
						cell.Attributes.Add("style", cell.Attributes["style"] + ";background-color:" + BackColor);
					}

					//add forecolor
					if(ForeColor != "none")
					{
						cell.Attributes.Add("style", cell.Attributes["style"] + ";color:" + ForeColor);
					}

					break;
				case ListItemType.Header:
					cell.CssClass=_exGroupName;
					if(_drawBorders)
					{
						cell.Attributes.Add("style", "display:none");
					}
					else
					{
						cell.Attributes.Add("style", "display:none;border-bottom-style=solid;border-left-style=none;border-right-style=none;border-top-style=none");
					}

					//add backcolor
					if(BackColor != "none")
					{
						cell.Attributes.Add("style", cell.Attributes["style"] + ";background-color:" + BackColor);
					}

					//add width
					if(Width != -1)
					{
						cell.Attributes.Add("style", cell.Attributes["style"] + ";width:" + Width.ToString() + "px");
					}

					break;
				case ListItemType.Footer:
					cell.CssClass=_exGroupName;
					if(_drawBorders)
					{
						cell.Attributes.Add("style", "display:none");
					}
					else
					{
						cell.Attributes.Add("style", "display:none;border-bottom-style=solid;border-left-style=none;border-right-style=none;border-top-style=solid");
					}

					//add backcolor
					if(BackColor != "none")
					{
						cell.Attributes.Add("style", cell.Attributes["style"] + ";background-color:" + BackColor);
					}

					break;
				case ListItemType.EditItem:
					break;
			}
		}
	}

	/// <summary>
	/// The ExColumnExpand is derived from the BoundColumn and contains an image with a plus/minus icon 
	/// on the header of the column. It is used to expand the ExColumn column class
	/// </summary>
	public class ExColumnExpand : System.Web.UI.WebControls.BoundColumn 
	{
		#region Property: ExGroupName
		private string _exGroupName;

		/// <summary>
		/// Specifies the column name that is the basis for template filename <see cref="TemplateCachingBase">caching</see>
		/// </summary>
		public string ExGroupName
		{
			get { return _exGroupName; }
			set { _exGroupName = value; }
		}
		#endregion Property: ExGroupName

		/// <summary>
		/// Initializes a new instance of ExColumnExpand class.
		/// </summary>
		public ExColumnExpand() : base()
		{
		}

		/// <summary>
		/// On initialization the ExColumnExpand adds a plus image to right of the header
		/// </summary>
		/// <param name="cell"></param>
		/// <param name="columnIndex"></param>
		/// <param name="itemType"></param>
		public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
		{
			string divCssClass = String.Empty;

			base.InitializeCell(cell, columnIndex, itemType);

			switch(itemType)
			{
				case ListItemType.Header:
					Image image = new Image();
					image.ID = "Icon";
					image.ImageUrl = "~/images/plus.gif";
					image.Attributes.Add("onClick", "javascript:ToggleExpand('" + _exGroupName + "', this, '" + this.Owner.ID + "');");
					image.ID = this._exGroupName.ToString() + "_ctl_Icon";
					Label headerText = new Label();
					headerText.Text = cell.Text;

					cell.Controls.Add(headerText);
					cell.Controls.Add(image);

					break;
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect Frontline Direct Inc., Adconion
United States United States
Tony Truong graduated from UCLA in Spring of 2001 and starting worked at Symantec Corporation as a Software Engineer. After a few years of developing various features for Norton SystemWorks, Tony moved to San Diego. He is currently writing database applications using ASP.NET and C# with the .NET Framework. Tony specializes in tara-byte databases with emphasis on high availability, optimization, and complex entity modeling.

Comments and Discussions