Click here to Skip to main content
Click here to Skip to main content

ASP.NET Extended DataGrid UI Features

, 29 Aug 2004
Rate this:
Please Sign up or sign in to vote.
The ASP.NET DataGrid Server Control is a versatile control. The control displays data bound information in a HTML table. There are several key UI features that the DataGrid does not have. For instance, many HTML tables that show data in a list, change the color of the row when the mouse hovers over.

Extended DataGrid UI Features Introduction

The ASP.NET DataGrid Server Control is a versatile control. The control displays data bound information in an HTML table. There are several key UI features that the DataGrid does not have. For instance, many HTML tables that show data in a list change the color of the row when the mouse hovers over. Also, when a list can be sorted, there is usually a tooltip text for each of the column headers that state "Sort by this column". This article shows how to incorporate these common features into a DataGrid.

The Facade

To reduce the complexity and redundancy for some of the interface methods, I created a facade called the WebUIFacade. A facade is a OO Design Pattern where you create a class that provides a simplified interface. There are several methods that extend the features of a DataGrid in the facade.

*** Note *** The SetRowHover method requires that you have a style named gridHover with a background color set to the color you want the row hover color to be.

Facade Code Example

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace YourProject.WebApp
{
    /// <SUMMARY>
    /// Summary description for WebUIFacade.
    /// </SUMMARY>
    public class WebUIFacade
    {
        /// <SUMMARY>
        /// Constructor.
        /// </SUMMARY>
        public WebUIFacade()
        {
            
        }

        /// <SUMMARY>
        /// This method creates a tooltip for the header columns in a datagrid.  
        /// Note:  This should only be used when the grid has sorting enabled.
        /// </SUMMARY>
        /// <PARAM name="e">DataGridItemEventArgs</PARAM>
        public void 
           SetHeaderToolTip(System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            // Is the item type of type header?
            if (e.Item.ItemType == ListItemType.Header)
            {
                string headerText = "";
                // Add the onmouseover and onmouseout
                // attribute to each header item.
                foreach (TableCell cell in e.Item.Cells)
                {
                    try
                    {
                        LinkButton lb = (LinkButton) cell.Controls[0];
                        headerText = "";

                        if(lb != null)
                        {
                            headerText = lb.Text;
                        }
                        
                        lb.ToolTip = "Sort By " + lb.Text;
                    }
                    catch{}
                }
            }
        }
    
        /// <SUMMARY>
        /// This method changes the color of the row when the mouse is over it.
        /// Note: You must have a class called gridHover
        ///       that sets the color of the hover row.
        /// </SUMMARY>
        /// <PARAM name="dg">DataGrid</PARAM>
        /// <PARAM name="e">DataGridItemEventArgs</PARAM>
        public void SetRowHover(DataGrid dg, 
            System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            try
            {
                string className = "";

                // Is the item an item or alternating item?
                if((e.Item.ItemType == ListItemType.Item) || 
                    (e.Item.ItemType == ListItemType.AlternatingItem))
                {
                    // Is the itemtype of type item?
                    if (e.Item.ItemType == ListItemType.Item)
                    {
                        className = dg.ItemStyle.CssClass;
                    }
                    else if(e.Item.ItemType == ListItemType.AlternatingItem)
                    {
                        className = dg.AlternatingItemStyle.CssClass;
                    }

                    e.Item.Attributes.Add("onmouseover", 
                             "this.className='gridHover';");
                    e.Item.Attributes.Add("onmouseout", 
                             "this.className='" + className + "';");
                }
            }
            catch
            {
            }
        }
        /// <SUMMARY>
        /// This method sets the CssStyle for a link button
        /// contained in the datagrid item, alternatingitem,
        /// or edititem row.  
        /// </SUMMARY>
        /// <PARAM name="e">DataGridItemEventArgs</PARAM>
        public void 
          SetGridLinkButtonStyle(System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || 
                e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.EditItem)
            {
                foreach(TableCell cell in e.Item.Cells)
                {
                    try
                    {
                        LinkButton lb = (LinkButton) cell.Controls[0];
        
                        if(lb != null)
                        {
                            lb.CssClass = "GridLink";
                        }
                    }
                    catch{}
                }
            }
        }

    }
}

The Hover Style

This is the style used by the WebUIFacade.SetRowHover method.

.gridHover
{
    background-color: #ffffcc;
}

DataGrid Code Behind

In order to use the methods provided by the WebUIFacade, you must instantiate the facade in the ItemCreated event of the DataGrid. You will then have access to the facade's publicly available methods.

private void dataGrid_ItemCreated(object sender, 
         System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    // Create a new WebUIFacade.
    WebUIFacade uiFacade = new WebUIFacade();
    
    // This is gives a tool tip for each
    // of the columns to sort by.
    uiFacade.SetHeaderToolTip(e);
    
    
    // This sets a class for the link buttons in a grid.
    uiFacade.SetGridLinkButtonStyle(e);
    
    // Make the row change color when the mouse hovers over.
    // *** You must have a class called gridHover with a different background 
    // color in your StyleSheet.
    uiFacade.SetRowHover(this.dataGrid, e);
}

Using the Code

  1. Create a new class in the Web Project called WebUIFacade.
  2. Copy the code for the WebUIFacade and paste it into your class. Ensure the namespace is the same as that of your web page. If not, include the facade in your web page references.
  3. Create a new web page.
  4. Add a DataGrid to the page and call it dataGrid.
  5. Copy the code from the ItemCreated event and place it in your page's code behind. Make sure the event is fired by the DataGrid.
  6. Add the style to a CSS file and include it in your web page, or add the style to the head of your page.

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

Share

About the Author

Kenny Young

United States United States
Ken currently works as the Director of Software Engineering at the Pediatrics Epidemiology Center at the University of South Florida. He is involved in the architecture and design on numerous clinical trial projects. Some of the project include:
 
1. Rare Diseases Clinical Research Network
2. TrialNet - Diabetes
3. Teddy - Diabetes
4. CCOP - Cancer
5. AIDA - Diabetes

Comments and Discussions

 
GeneralExcellent Pinmemberalam_pune20-Mar-06 11:51 
Generalwhy use linkbutton Pinmemberscow14-Nov-05 23:32 
Questionwhy create new instance when you can reference with &quot;this&quot;? Pinmembercoderchen14-Oct-05 13:49 
AnswerRe: why create new instance when you can reference with &quot;this&quot;? PinsussAnonymous14-Oct-05 14:45 
GeneralRe: why create new instance when you can reference with &quot;this&quot;? Pinmembercoderchen21-Apr-06 16:35 
Generalreal poor idea Pinmemberdajianshi4-Jul-05 18:42 
GeneralRe: real poor idea PinsussAnonymous5-Jul-05 2:13 
GeneralRe: real poor idea PinmemberKenny Young5-Jul-05 3:53 
GeneralInheritance PinsussAnonymous30-Aug-04 12:17 
GeneralRe: Inheritance PinmemberKen G Young30-Aug-04 12:44 
GeneralRe: Inheritance PinsussAnonymous30-Aug-04 17:25 
GeneralRe: Inheritance PinmemberKen G Young31-Aug-04 4:41 
GeneralRe: Inheritance Pinmemberhejo756-Apr-05 10:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Aug 2004
Article Copyright 2004 by Kenny Young
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid