Thursday, April 8, 2010

Make gridview selectable by clicking and changing the cursor on hover

Here is the code to change the cursor as you hover over a gridview row
and enable it to be selected by clicking the row:

protected void gv1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.cursor='hand'");
e.Row.Attributes.Add("onmouseout",
"this.style.cursor='cursor'");
}
}

protected void gv1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick",
Page.ClientScript.GetPostBackEventReference(gv1, "Select$" +
Convert.ToString(e.Row.RowIndex)));
}
}

Enjoy.

No comments: