Highlighting Gridview Row when it has Alternating Row Color.

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 1260
Suppose,we have a Gridview called grid_project_details as below:-
<asp:GridView ID = "grid_project_details" runat = "server" 
OnRowCreated = "grid_project_details_RowCreated">
</asp:GridView>


Now in the OnRowCreated event we have to write below code:-

protectedvoid grid_project_details_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//On Mouse Over Highlight Row set row color to yellow
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='yellow'");

//On Mouse out reset girdview row color based on our alternate row color
if (e.Row.RowIndex % 2 == 0)
{
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='lightyellow'");
}
else
{
//set white color as default color
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
}
}
}

Comments or Responses

Login to post response