Saturday, January 11, 2014

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Controls_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">
     <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" />
                <Columns>
                    <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"  />
                </Columns>
            </asp:GridView>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class Controls_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Emp_Name");
        dt.Columns.Add("Emp_id");
        dt.Columns.Add("Emp_job");
        dt.Columns.Add("Emp_Dep");
        DataRow rw1 = dt.NewRow();
        rw1["Emp_Name"] = "Narain Sidharth";
        rw1["Emp_id"] = "2020";
        rw1["Emp_job"] = "SOftware Engineer";
        rw1["Emp_Dep"] = "IT";
        dt.Rows.Add(rw1);
        DataRow rw2 = dt.NewRow();
        rw2["Emp_Name"] = "Prakalathan";
        rw2["Emp_id"] = "1978";
        rw2["Emp_job"] = "System Engineer";
        rw2["Emp_Dep"] = "IT";
        dt.Rows.Add(rw2);
        DataRow rw3 = dt.NewRow();
        rw3["Emp_Name"] = "Mathu kumar";
        rw3["Emp_id"] = "2700";
        rw3["Emp_job"] = "Support Enginner";
        rw3["Emp_Dep"] = "IT";
        dt.Rows.Add(rw3);
        DataRow rw4 = dt.NewRow();
        rw4["Emp_Name"] = "Arvind";
        rw4["Emp_id"] = "4678";
        rw4["Emp_job"] = "Sr Software Engineer";
        rw4["Emp_Dep"] = "IT";
        dt.Rows.Add(rw4);
        DataRow rw5 = dt.NewRow();
        rw5["Emp_Name"] = "Raja ram";
        rw5["Emp_id"] = "2300";
        rw5["Emp_job"] = "Test Engineer";
        rw5["Emp_Dep"] = "IT";
        dt.Rows.Add(rw5);
        GridVwRowColorchange.DataSource = dt;
        GridVwRowColorchange.DataBind();
    }
    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");
        }
    }
}

MouseOver

0 comments:

Post a Comment