Home > Articles > ASP.NET Articles > How to change gridview row color on mouse over in asp.net?

How to change gridview row color on mouse over in asp.net?

by Thamil   on Jul 10, 2013   Category: ASP.NET  | Level: Beginner  |  Views: 9777    |  Points: 100   
Like this article? Bookmark and Share:
In this article we will see how to change gridview row color on mouse over in asp.net applications.


Sometime we may need this kind of functionality, changing row color on mouse over and mouse out in asp.net projects. Actually there is no built in features to modify the color on mouse over and out in gridview control and we need to write code for this explicitly. See the below in a simple way how to change the gridview row color.

Step1: Create asp.net project

Drag the grdivew control and design as shown below.

            <asp:GridView ID="GridVwRowColorchange" runat="server" AutoGenerateColumns="False"
                Font-Names="Verdana" PageSize="5" Width="75%"
                BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" onrowdatabound="GridVwRowColorchange_RowDataBound" >
                <AlternatingRowStyle BackColor="#BFE4FF" />
                <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
                <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
                    BorderStyle="Solid" BorderWidth="1px" />
                <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
                    BorderWidth="1px" />
                    <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name"  />
                    <asp:BoundField DataField="Emp_id" HeaderText="Employee ID"  />
                    <asp:BoundField DataField="Emp_job" HeaderText="Job title"  />
                    <asp:BoundField DataField="Emp_Dep" HeaderText="Department"  />


Step2: Page load event

To display the data in gridview control, here I have create the datatable and added few rows and data to display in gridview control. Copy and paste the below code into code behind part

protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
        private void LoadData()
            DataTable dt = new DataTable();
            DataRow rw1 = dt.NewRow();
            rw1["Emp_Name"] = "Narain Sidharth";
            rw1["Emp_id"] = "2020";
            rw1["Emp_job"] = "SOftware Engineer";
            rw1["Emp_Dep"] = "IT";
            DataRow rw2 = dt.NewRow();
            rw2["Emp_Name"] = "Prakalathan";
            rw2["Emp_id"] = "1978";
            rw2["Emp_job"] = "System Engineer";
            rw2["Emp_Dep"] = "IT";
            DataRow rw3 = dt.NewRow();
            rw3["Emp_Name"] = "Mathu kumar";
            rw3["Emp_id"] = "2700";
            rw3["Emp_job"] = "Support Enginner";
            rw3["Emp_Dep"] = "IT";
            DataRow rw4 = dt.NewRow();
            rw4["Emp_Name"] = "Arvind";
            rw4["Emp_id"] = "4678";
            rw4["Emp_job"] = "Sr Software Engineer";
            rw4["Emp_Dep"] = "IT";
            DataRow rw5 = dt.NewRow();
            rw5["Emp_Name"] = "Raja ram";
            rw5["Emp_id"] = "2300";
            rw5["Emp_job"] = "Test Engineer";
            rw5["Emp_Dep"] = "IT";
            GridVwRowColorchange.DataSource = dt;


Step3: RowDataBound event

This gridview rowdatabound event occurs when row is bound to data in gridview control, so this is the place where we need to modify the row color when mouse and mouse out. See the below code.

 protected void GridVwRowColorchange_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Attributes.Add("onmouseover", "self.MouseOverOldColor=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=self.MouseOverOldColor");

In the above code, I’m changing the row color on mouse over and same assigning old background color when mouse out.

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

Like this article? Bookmark and Share:

Most viewed Articles

User Comments

No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts

1 How to Set Line Spacing of a Paragraph in a Shape or Textbox inside .NET Ap... New! Code
2 Fix to : Custom Error Page While Accessing ASP.NET Site Remotely From Server New! Article
3 Simple steps to Map the User Friendly Host Name With Its Corresponding IP Address Using Host File In... New! Article
4 Simple ASP.NET Code to Find All the SQL Statements Used in a Project New! Article
5 Simple steps to Convert Code Behind Files or Class Files into One dll File in Visual Studio Article
6 Simple Example to Bind Tree View in Asp.net C# without recursive functions Article
7 Begineers Guide :Simple Example to understand JavaScript Function onblur() and onfocus() in ASP.NET ... Article
8 How to add and format SmartArt shape on PowerPoint document Article
9 Fix To: Retrieving the COM Class Factory for Component with CLSID Error Article
10 Import and Export Excel Files into Varbinary (Max) in SQL Server Article