Friday, 24 July 2009

How to highlight a GridView's row on mouse hover in

With small amount of JavaScript we can correctly highlight the GridView row when user hovers over each row. This can be very helpful when we have a very wide report. To achieve this, we can use the technique I discussed in the previous article: Calling a JavaScript function from server side code.

The following JavaScript can do the trick, please note this also works well if you have set the AlternatingRowStyle of the GridView.

function highlight(tableRow, active) {
if (active) {
tableRow.originalstyle =; = '#cfc';
else { = tableRow.originalstyle;

Now we can drag a GridView to the page and selected a valid data source. In this demo, I use the Order table in Northwind database. In order to highlight the row on mouse hover, we need to add some custom JavaScript code to onmouseover and onmouseout event handlers of the rows of our GridView. In codehind we have:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
e.Row.Attributes["onmouseover"] = "highlight(this, true); = 'pointer'";
e.Row.Attributes["onmouseout"] = "highlight(this, false);";

Run the application, you will see the result as the screenshot below:

No comments:

Post a Comment