In this article I will explain how to highlight row on mouse
over in Gridview using asp.net
I created a webform with name EmpInfo.aspx
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="EmpInfo.aspx.cs"
Inherits="BindGrid.EmpInfo"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
#gridview tr.Hover:hover
{
background-color:#00FF00
;
}
</style>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID = "gridview" runat
= "server"
AutoGenerateColumns =
"false" RowStyle-CssClass
= "Hover"
>
<Columns>
<asp:BoundField DataField = "EmpId" HeaderText
= "EmpId"
/>
<asp:BoundField DataField = "EmpName" HeaderText
= "EmpName"
/>
<asp:BoundField DataField = "EmpAddress" HeaderText
= "EmpAddress"
/>
<asp:BoundField DataField = "EmpDesignation" HeaderText
= "EmpDesignation"
/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
In EmpInfo.aspx.cs I am writing the logic to display data in
gridview.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace BindGrid
{
public partial class EmpInfo :
System.Web.UI.Page
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["dotnetdbConnectionString"].ToString());
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindGrid();
}
}
public void
bindGrid()
{
SqlCommand cmd = new
SqlCommand("select
* from Employee",con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
gridview.DataSource = dr;
gridview.DataBind();
con.Close();
}
}
}
}
In web.config I am adding the connecting string
<connectionStrings>
<add name="dotnetdbConnectionString" connectionString="Data
Source=CHINNU;Initial Catalog=dotnetdb;User ID=sa;Password=password123" providerName="System.Data.SqlClient" />
</connectionStrings>
Finally press F5 we can get the out put as shown
.