For example, a common task is to highlight gridview row on mouse over which is not provided with gridview control. Here we will see how easily we can do the task.
In order to change gridview row color we need to add/remove style attributes to that specific row using JavaScript onmouseover and onmouseout client event. We can do it on RowDataBound or RowCreated gridview event.
Code Snippet:
protected void gvHrEmploye_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight color
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFFAA'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
}
}
or,
protected void gvHrEmploye_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight color
e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFFAA'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
}
}
It works properly even if you set AlternatingRowStyle property or the row is previously selected.
How it works:
Nice post Mr. Saiful Islam.
ReplyDeleteThis trick has been asked by many users troughout the Microsoft ASP.NET Forums and it is great to have it as blogpost in order to put reference for faster answers to other members ;).
Thanks,
Hajan