Changing Row Color of the GridView Control on mouse over

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.

Print | posted @ Tuesday, April 11, 2006 8:13 PM

Twitter