Click here to Skip to main content
Licence 
First Posted 24 May 2004
Views 154,021
Downloads 2,742
Bookmarked 51 times

Datagrid custom control to change the row color on Mouse Over event

By Albert Pascual | 24 May 2004
Datagrid control with mouse over event to change the row color as Code Project
2 votes, 14.3%
1
1 vote, 7.1%
2
1 vote, 7.1%
3
1 vote, 7.1%
4
9 votes, 64.3%
5
3.68/5 - 14 votes
μ 3.68, σa 2.75 [?]

Introduction

This article describes another DataGrid control. Datagrids are very flexible, this one just inherits from the class DataGrid and adds the mouse over event to change the color of the row as the Code Project tables. Got the idea from Chris Maunder tables on Code Project. I looked at his JavaScript and I implemented into the .NET DataGrid class.

Using the code

This is a customer control. I include the source code for the control and a test project how to use it. As any customer control, you need to add the reference to the project. On the ASPX page:

(...)
< MOD:MODataGrid> id="DataGrid1" style="Z-INDEX: 101; 
  LEFT: 320px; POSITION: absolute; TOP: 136px"
  runat="server">

On the aspx.cs file add:

  protected MouseOverDataGrid.MODataGrid DataGrid1;  

I used the Datagrid tutorial example from http://asp.net for the test project

The Control

A simple Inherit from Datagrid class and overriding the Render function with Chris Maunder JavaScript. There are 2 public properties, the SpecialLinkColor as default set to CodeProject mouseover color that you can set to any color you like. And OverLinkHand to display the cursor as a Hand. I did not implement the method for on-click but it is on the JavaScript, so you can always implement it.

 [assembly:TagPrefix("MouseOverDataGrid", "MOD")]
namespace MouseOverDataGrid
{
 /// <summary>
 /// Summary description for MouseOverDataGrid.
 /// </summary>
 public class MODataGrid : System.Web.UI.WebControls.DataGrid
 {  

  [Category("Design"), Description("OverLink Color")]
  private string sBackgroundColor = "#dddddd";
  public string SpecialLinkColor
  {
   get{return(sBackgroundColor);}
   set{sBackgroundColor=value;}
  }

  [Category("Design"), Description("OverLink Hand Type")]
  private bool bOverLinkHand = false;
  public bool OverLinkHand
  {
   get{return(bOverLinkHand);}
   set{bOverLinkHand=value;}
  }

  protected override void Render(System.Web.UI.HtmlTextWriter writer)
  {
   string sDataGridID = this.ID.ToString();

   string sCursor = "";
   if ( OverLinkHand == true )
    sCursor = "elm.style.cursor = Cursor;\r";

   string sJavaScript = "< script language="'\""javascript\"'>\r" +
    "function ShowRow1(elm, BgColour, FgColour)\r" +
    "{\r" +
    "elm.bgColor = BgColour;\r" + sCursor +
    "}\r" +
    "function HilightRow(elm, hover, highlight)\r" +
    "{\r" +
    " var BgColour = (hover)? \"" + SpecialLinkColor + "\" :  \"white\";\r" +
    " var FgColour = \"black\";\r" +
    " var Cursor   = (hover)? \"hand\" :  \"auto\";\r" +
    " ShowRow1(elm, BgColour, FgColour);\r" +
    " return false;\r" +
    "}\r" +
    "function RowOn()  { return HilightRow(this, true, false);  }\r" +
    "function RowOff() { return HilightRow(this, false, false); }\r" +
    "function RowClick()\r" +
    "{\r" +
    " HilightRow(this, false, true);\r" +
    " var elm = eval('document.getElementById(\"' + this.name + '_link\")');\r" +
    " if (elm) elm.click();\r" +
    " return true;\r" +
    "}\r" +
    "var table = document.getElementById(\"" + sDataGridID + "\");\r" +
    "var rows = table.getElementsByTagName(\"tr\");\r" +
    "for (var i = 0; i < rows.length; i++) {\r" +
    "rows[i].onmouseover = RowOn;\r" +
    "rows[i].onmouseout  = RowOff;\r" +
    //"rows[i].onclick     = RowClick;" +
    "}\r</script>\r";
  


   base.Render (writer);   
   base.Page.Response.Write(sJavaScript);
  }

 }
  

Points of Interest

For the Datagrid to work you have to make sure you do not add any color on the item background. If you are using paging, add same color in the footer.

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

About the Author

Albert Pascual

Web Developer

United States United States

Member
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.
 
Al is a Microsoft ASP.NET MVP
 
Blog

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to implement paging in your Datagrid custom control Pinmemberkarunakarrao2:09 23 Feb '05  
GeneralAnother Implementation Pinsuss< The Code Provider />6:50 19 Jul '04  
GeneralRe: Another Implementation Pinsuss< The Code Provider />7:15 19 Jul '04  
GeneralAnother Implementation PinsussThe Code Provider6:18 19 Jul '04  
GeneralSeems awful complicated for such a simple task PinmemberShamusFu8:34 16 Jul '04  
GeneralAnother Approach PinsussBrian Nash6:18 16 Jul '04  
GeneralRe: Another Approach PinsussBrian Nash6:21 16 Jul '04  
GeneralRe: Another Approach Pinmemberttestingonly9:15 16 Jul '04  
Questionif used two controls,how to? Pinmemberlinhaiyuan20:29 27 Jun '04  
GeneralHandle right click Pinmemberenjoycrack17:00 16 Jun '04  
GeneralEnter key to work like tab key Pinmemberrags4:34 2 Jun '04  
GeneralRe: Enter key to work like tab key Pinmemberjbrink500016:26 28 Jun '06  
GeneralWhy separate control PinmemberBee Master20:47 24 May '04  
GeneralRe: Why separate control Pinmemberzaf0:03 25 May '04  
GeneralRe: Why separate control Pinmemberpmpjr7:36 3 Jun '04  
If you are going to use this method, then just grab the backgroundColor before changing it and put it into a temp string so you can reuse it as follows:
 

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string strTempColor = (e.Item.ItemType == ListItemType.Item) ? ColorTranslator.ToHtml(DataGrid1.ItemStyle.BackColor) : ColorTranslator.ToHtml(DataGrid1.AlternatingItemStyle.BackColor) ;
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='powderblue'; this.style.cursor='hand';");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + strTempColor + "';");
}

 
There is no place like 127.0.0.1!
GeneralRe: Why separate control PinmemberAlbert Pascual6:19 25 May '04  
GeneralRe: Why separate control Pinmemberspvarapu1:51 25 May '05  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 25 May 2004
Article Copyright 2004 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid