Friday, July 17, 2009

Highlight Gridview row when mouse hover.

purpose: when mouse hover to girview row it will highlight the row..
controls: Add one gridview

connection object....
SqlConnection con = new SqlConnection("Data Source=SUSANT\\SQLEXPRESS ;Initial Catalog=Infitech; Integrated Security=True;");

//Page Load Event...
protected void Page_Load(object sender, EventArgs e)
{

show_data();

}
//select all the records from database..
public void show_data()
{
try
{
if (con.State==ConnectionState.Open)
{
con.Close();
}
con.Open();
DataSet ds = new DataSet();
string str = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(str, con);
da.Fill(ds, "temp");

if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}

//gridview RowCreated Event....
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
string onmouseoverStyle = "this.style.backgroundColor='cyan'";
string onmouseoutStyle = "this.style.backgroundColor='white'";
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", onmouseoverStyle);
e.Row.Attributes.Add("onmouseout", onmouseoutStyle);
}
}

No comments:

Bookmark and Share