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

Better DataGrid Column Header ToolTips

Rate me:
Please Sign up or sign in to vote.
3.70/5 (11 votes)
15 Jun 2006CPOL1 min read 47.9K   146   17  
Provides a technique to implement column header tooltips in a datagrid.
using System;
using System.Web.UI.WebControls;

namespace samplewebapp
{
	public class CustomDataGrid : DataGrid
	{
		private const string HEADER_TOOLTIP_FORMAT = @"<p title=""{0}"">{1}</p>";

		/// <summary>
		/// Adds tooltip capability to column headers
		/// </summary>
		/// <param name="e">DataGridItemEventArgs</param>
		protected override void OnItemCreated(DataGridItemEventArgs e)
		{
			if(e.Item.ItemType == ListItemType.Header) 
			{ 
				for (int i=0;i < e.Item.Cells.Count;i++) 
				{
					if (Columns[i] is CustomBoundColumn) 
					{
						CustomBoundColumn col = (CustomBoundColumn)Columns[i];

						if (col.HeaderToolTip != null) 
						{
							TableCell cell = e.Item.Cells[i];
							cell.Text = String.Format(HEADER_TOOLTIP_FORMAT,col.HeaderToolTip,cell.Text);
						}
					}
				}	
			}

			//invoke base class method
			base.OnItemCreated (e);
		}
	}
}

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)


Written By
Software Developer (Senior)
United States United States
Seth Deckard has been a software developer in the industry since 1999.

Comments and Discussions