Change ASP.Net Gridview Row Color on Mouseover

Sometimes we may need this due to requirements, the ability to change a row's color on mouse over and mouse out in ASP.Net projects. See the following simple sample of changing the grid view row color.

Run the project.



Then click any row index to see its details.



First of all you need to create an "ASP.NET Empty Web Site". Then use the following procedure.

Step 1: Create an EmployeeRecord class in the App_Code folder and provide the following:

  1. using System.Data;  
  2.   
  3. public class EmployeeRecord  
  4. {  
  5.    public DataTable EmpRecord()  
  6.    {  
  7.       DataTable dt = new DataTable();  
  8.       dt.Columns.Add("Emp_Name");  
  9.       dt.Columns.Add("Emp_id");  
  10.       dt.Columns.Add("Emp_job");  
  11.       dt.Columns.Add("Emp_Dep");  
  12.       DataRow rw1 = dt.NewRow();  
  13.       rw1["Emp_Name"] = "Arvid Gaur";  
  14.       rw1["Emp_id"] = "00011";  
  15.       rw1["Emp_job"] = "Software Engineer";  
  16.       rw1["Emp_Dep"] = "IT";  
  17.       dt.Rows.Add(rw1);  
  18.       DataRow rw2 = dt.NewRow();  
  19.       rw2["Emp_Name"] = "Dharmender kumar";  
  20.       rw2["Emp_id"] = "00024";rw2["Emp_job"] = "System Engineer";  
  21.       rw2["Emp_Dep"] = "IT";  
  22.       dt.Rows.Add(rw2);  
  23.       DataRow rw3 = dt.NewRow();  
  24.       rw3["Emp_Name"] = "Ravi kumar";  
  25.       rw3["Emp_id"] = "00138";  
  26.       rw3["Emp_job"] = "Support Enginner";  
  27.       rw3["Emp_Dep"] = "IT";  
  28.       dt.Rows.Add(rw3);  
  29.       DataRow rw4 = dt.NewRow();  
  30.       rw4["Emp_Name"] = "Arvind Kumar";  
  31.       rw4["Emp_id"] = "00058";  
  32.       rw4["Emp_job"] = "Sr Software Engineer";  
  33.       rw4["Emp_Dep"] = "IT";  
  34.       dt.Rows.Add(rw4);  
  35.       DataRow rw5 = dt.NewRow();  
  36.       rw5["Emp_Name"] = "Rajan Sharma";  
  37.       rw5["Emp_id"] = "00085";  
  38.       rw5["Emp_job"] = "Test Engineer";  
  39.       rw5["Emp_Dep"] = "IT";  
  40.       dt.Rows.Add(rw5);  
  41.       return dt;  
  42.    }  
  43. }  

Step 2: Insert the grid view control intp the Default.aspx page then write the following design code:

  1. <asp:GridView ID="ui_grdVw_EmployeeDetail" runat="server" Width="50%" AutoGenerateColumns="false"              HeaderStyle-CssClass="pageheading" OnRowDataBound="ui_grdVw_EmployeeDetail_RowDataBound" OnSelectedIndexChanged="ui_grdVw_EmployeeDetail_SelectedIndexChanged">  
  2. <Columns>  
  3.    <asp:TemplateField HeaderText="S.NO">  
  4.    <ItemTemplate>    
  5.        <%#Container.DataItemIndex+1 %>  
  6.    </ItemTemplate>  
  7.    </asp:TemplateField>  
  8.   
  9.    <asp:TemplateField HeaderText="Employee ID">  
  10.    <ItemTemplate>  
  11.       <asp:Label ID="ui_lbl_EmployeeID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Emp_id") %>'></asp:Label>  
  12.    </ItemTemplate>  
  13.    </asp:TemplateField>  
  14.   
  15.    <asp:TemplateField HeaderText="Employee Name">  
  16.    <ItemTemplate>  
  17.       <asp:Label ID="ui_lbl_EmployeeName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Emp_Name") %>'></asp:Label>  
  18.    </ItemTemplate>  
  19.    </asp:TemplateField>  
  20.   
  21.    <asp:TemplateField HeaderText="Employee Post">  
  22.    <ItemTemplate>  
  23.       <asp:Label ID="ui_lbl_EmpJob" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Emp_job") %>'></asp:Label>  
  24.    </ItemTemplate>  
  25.    </asp:TemplateField>  
  26.   
  27.    <asp:TemplateField HeaderText="Department">  
  28.    <ItemTemplate>  
  29.       <asp:Label ID="ui_lbl_Department" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Emp_Dep") %>'></asp:Label>  
  30.    </ItemTemplate>  
  31.    </asp:TemplateField>  
  32. </Columns>  
  33. </asp:GridView>  
  34. <asp:LinkButton ID="ui_lnkbtn_Dummy" runat="server"></asp:LinkButton>  
Step 3: Now, open the Default.aspx.cs then write the following code:
  1. using System;  
  2. using System.Web.UI;  
  3. using System.Web.UI.WebControls;  
  4. using System.Data;  
  5. using System.Text;  
  6.   
  7. public partial class _Default : System.Web.UI.Page  
  8. {  
  9.    protected void Page_Load(object sender, EventArgs e)  
  10.    {  
  11.       if (!IsPostBack)  
  12.       {  
  13.          ui_grdvw_EmployeeDetail_Bind();  
  14.       }  
  15.    }  
  16.      
  17.    protected void ui_grdvw_EmployeeDetail_Bind()  
  18.    {  
  19.       DataTable dt = new DataTable();  
  20.       EmployeeRecord employeeRecord = new EmployeeRecord();  
  21.       dt = employeeRecord.EmpRecord();  
  22.       ui_grdVw_EmployeeDetail.DataSource = dt;  
  23.       ui_grdVw_EmployeeDetail.DataBind();  
  24.    }  
  25.   
  26.    protected void ui_grdVw_EmployeeDetail_RowDataBound(object sender, GridViewRowEventArgs e)  
  27.    {  
  28.       if (e.Row.RowType == DataControlRowType.DataRow)  
  29.       {  
  30.          e.Row.Attributes.Add("onmouseover",  
  31.                this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#0071b6';this.styl               e.color='White'");  
  32.          e.Row.Attributes.Add("onmouseout",                     "this.style.backgroundColor=this.originalstyle;this.style.color=this.originalstyle;");  
  33.          e.Row.Attributes["onclick"] =          Page.ClientScript.GetPostBackClientHyperlink(ui_grdVw_EmployeeDetail, "Select$" + e.Row.RowIndex);  
  34.          e.Row.Attributes["style"] = "cursor:pointer";  
  35.       }  
  36.     }  
  37.   
  38.     protected void ui_grdVw_EmployeeDetail_SelectedIndexChanged(object sender, EventArgs e)  
  39.     {  
  40.          Label Emp_id = (Label)ui_grdVw_EmployeeDetail.SelectedRow.FindControl("ui_lbl_EmployeeID");  
  41.          Label Emp_Name = (Label)ui_grdVw_EmployeeDetail.SelectedRow.FindControl("ui_lbl_EmployeeName");  
  42.          Label Emp_job = (Label)ui_grdVw_EmployeeDetail.SelectedRow.FindControl("ui_lbl_EmpJob");  
  43.          Label Emp_Dep = (Label)ui_grdVw_EmployeeDetail.SelectedRow.FindControl("ui_lbl_Department");  
  44.          StringBuilder alartMsg = new StringBuilder();  
  45.          alartMsg.Append("You are Selected:-\\n");  
  46.          alartMsg.Append("ID : " + Emp_id.Text+"\\nName : "+Emp_Name.Text+"\\nPost :                           "+Emp_job.Text+"\\nDepartment : "+Emp_Dep.Text);  
  47.           ScriptManager.RegisterStartupScript(thisthis.GetType(),           "scriptName","alert('"+alartMsg.ToString()+"');"true);  
  48.     }  
  49. }  
In the preceding code, I'm changing the row color on mouse over and doing the same to re-assign the old background color upon mouse out and a click to show the details in an alert.

I have attached the sample project for this, download and see how it works. Thank you for reading and I hope you enjoyed this blog.


Similar Articles