In stead of using default WebControls.GridView, we can create a custom HouseGridView as:
[ToolboxData("<{0}:HoverGridView runat=server></{0}:HoverGridView>")]
public class HoverGridView : System.Web.UI.WebControls.GridView
{
public bool MouseHoverRowHighlightEnabled
{
get
{
if (ViewState["MouseHoverRowHighlightEnabled"] != null)
return (bool)ViewState["MouseHoverRowHighlightEnabled"];
else
return false;
}
set { ViewState["MouseHoverRowHighlightEnabled"] = value; }
}
public Color RowHighlightColor
{
get
{
if (ViewState["RowHighlightColor"] != null)
return (Color)ViewState["RowHighlightColor"];
else
{
return Color.Yellow;
}
}
set { ViewState["RowHighlightColor"] = value; }
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (MouseHoverRowHighlightEnabled)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
string.Format("this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='{0}'",
ColorTranslator.ToHtml(RowHighlightColor)));
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
}
}
}
}