Thursday, June 15, 2006

South Florida Gated Community For Sale

ASP.NET GridView with Row Highlighting

order in a GridView row highlight color when you move the mouse over it, only a small effort is required.

First we create a control that is derived from GridView. Now we implement a property that determines whether RowHighlighting is desired and another property that specifies the color of highlighting.
 [ToolboxData ( "\u0026lt;{0}: HighlightedGridView runat = server ID = 
HighlightedGridView1> \u0026lt;/ {0}: HighlightedGridView>
)] public
class HighlightedGridView: GridView {

[Category ( "Behavior" )]
[DefaultValue ( "false" )]
public bool HighlightRowOnMouseOver
{
get { return ViewState[ "rHighlight" ] == null ?
false : ( bool )ViewState[ "rHighlight" ]; }
set { ViewState[ "rHighlight" ] = value ; }
}

[Category( "Appearance" )]
[Description( "---RowHighlightColor---" )]
public Color RowHighlightColor
{
get {return ViewState [ "hlColor" ] == null ?
Color.White: (Color) ViewState [ "hlColor" ]; set}
{ViewState [ "hlColor" ] = value ;}
}
}
Next, we override OnRowCreated and set the RowHighlightColor.
    protected override void    OnRowCreated 
(GridViewRowEventArgs e) {

base OnRowCreated (s).

if ( this HighlightRowOnMouseOver. & & E.Row.RowType == DataControlRowType.DataRow
)
SetRowHighlightColor (e.Row);}
Now we have to just create the method that handles the highlighting. I know here with BackColor always the background color. This can lead to undesirable effects when an alternating rowcolor has been set. But I leave the attentive reader.
 
private void SetRowHighlightColor (GridViewRow row) {

row.Attributes.Add ( "onmouseover" ,
"this.style.backgroundColor = '" +
Utils.ColorToHexString (RowHighlightColor) + "'" );
row.Attributes.Add ( "onmouseout" ,
"this.style.backgroundColor ='" +
Utils.ColorToHexString (BackColor) + "'" );
}

0 comments:

Post a Comment