Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.
Few months back I blogged about changing the Back Color of the row contained in the GridView control when you put your mouse cursor over it. In this blog entry I am doing the same thing more efficiently. Check out the code below:
 protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {       
        string onmouseoverStyle = "this.style.backgroundColor='blue'";
        string onmouseoutStyle = "this.style.backgroundColor='#@BackColor'"; 
        
        string rowBackColor = String.Empty;         

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (e.Row.RowState == DataControlRowState.Alternate)
                rowBackColor = MyGridView.AlternatingRowStyle

.BackColor.Name.Remove(0, 2);
            else rowBackColor =  MyGridView.RowStyle

.BackColor.Name.Remove(0, 2);
        
            
            e.Row.Attributes.Add("onmouseover", onmouseoverStyle);
            e.Row.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor",rowBackColor));                       
        }       
    }

One strange thing is that even if you GridView contains the AlternatingStyle BackColor as "#FFFFFF" then it always picks up "White" which is true but I needed the code "#FFFFFF". Anyways, the above code will get you started and will work as expected. Posted on Tuesday, April 11, 2006 8:13 PM | Back to top


Comments on this post: Changing Row Color of the GridView Control on mouse over

# re: Changing Row Color of the GridView Control on mouse over
Requesting Gravatar...
Here's some handy functions along these lines...


public static void AddMouseoverHighlight(GridViewRowEventArgs e, string HighlightColor, string NormalRowColor, string AlternateRowColor)
{
if (e.Row.DataItemIndex > -1)
{
e.Row.Attributes.Add("onMouseOver", "this.style.background='" + HighlightColor + "';");
if (e.Row.DataItemIndex % 2 == 0) { e.Row.Attributes.Add("onMouseOut", "this.style.background='" + NormalRowColor + "';"); } else { e.Row.Attributes.Add("onMouseOut", "this.style.background='" + AlternateRowColor + "';"); }
}
}

/// <summary>
/// Adds Mouseover Highlighting to rows in e's gridview
/// </summary>
/// <param name="e"></param>
public static void AddMouseoverHighlight(GridViewRowEventArgs e)
{
AddMouseoverHighlight(e, "#00ffff", "#ffffff", "#eeeeee");
}
Left by Brian Foote on Apr 11, 2006 6:44 PM

# re: Changing Row Color of the GridView Control on mouse over
Requesting Gravatar...
De esta forma me funciono correcto usando las alternativas planteadas, en lenguaje VB..

Dim HighlightColor, NormalRowColor, AlternateRowColor As String
HighlightColor = "#C1D9F1"
NormalRowColor = "#FFFFFF"
AlternateRowColor = "#D8E8F5"
If e.Row.DataItemIndex > -1 Then
e.Row.Attributes.Add("onMouseOver", "this.style.background='" + HighlightColor + "';this.style.cursor = 'pointer';")
e.Row.Attributes.Add("onClick", "MM_goToURL('parent','detallepag.aspx?opconsultapag=2&codigoregistro=" & codigo & "&tipo=Metas Intermedias&ctrsession=0" & "');return document.MM_returnValue")
If e.Row.RowState = DataControlRowState.Alternate Then 'e.Row.DataItemIndex / 2 = 0
e.Row.Attributes.Add("onMouseOut", "this.style.background='" + NormalRowColor + "';")
Else
e.Row.Attributes.Add("onMouseOut", "this.style.background='" + AlternateRowColor + "';")
End If
End If
Left by Romualdo Rijo on Jan 09, 2007 1:07 PM

# re: Changing Row Color of the GridView Control on mouse over
Requesting Gravatar...
check this site also for changing grid view color
Left by pallavis eth on Aug 19, 2010 6:05 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net