Set Style on Mouse Over
You can style a row or header when a user moves the mouse over it. Each embedded RadGrid skin provides a class such as GridRowOver_<SkinName> for hovered rows.
Example: Defining the embedded skin hover class
.GridRowOver_[SkinName]
{
background-color: orange;
cursor:pointer;
}
To enable the built-in row hover style, set ClientSettings.EnableRowHoverStyle to True.
The built-in hover style depends on the RadGrid client-side object. For performance reasons, the client object is available only when certain client features or events are enabled. If the grid has no client features or events, attach an empty JavaScript function to a client event such as
OnRowClick.
If you want to attain the same functionality without the built-in feature of RadGrid for ASP.NET AJAX or with disabled skins, the steps below show how to achieve this:
-
Create the Telerik RadGrid instance and the data source to which it will be bound.
-
Create a style class in the
headsection of the.aspxpage to style the active row or header:Example: Defining custom row and header hover styles
ASP.NET<style type="text/css"> .RowMouseOver td { background-color: lightblue !important; } .RowMouseOut { /*this style is taken from the corresponding skin's GridRow_[SkinName] class - GridRow_Default in our case*/ background: #f7f7f7; } .HeaderMouseOver { background-color: lightblue !important; } .HeaderMouseOut { /*this style is taken from the corresponding skin's th.GridHeader class - th.GridHeader_Default in our case*/ background: white url('Img/GridHeaderBg.gif') repeat-x bottom; } </style> -
This approach relies on the OnRowMouseOver, OnRowMouseOut, OnColumnMouseOver, and OnColumnMouseOut client-side functions. Declare them in the ClientSettings > ClientEvents section of the grid declaration:
Example: Connecting RadGrid client events to hover handlers
ASP.NET<ClientSettings> <ClientEvents OnRowMouseOver="RowMouseOver" OnRowMouseOut="RowMouseOut" OnColumnMouseOver="ColumnMouseOver" OnColumnMouseOut="ColumnMouseOut" /> </ClientSettings> -
Before the grid tag on the page or in the
headsection, include the client-side JavaScript functions:Example: Handling RadGrid row and column hover events
JavaScript<script type="text/javascript"> function RowMouseOver(sender, eventArgs) { $get(eventArgs.get_id()).className = "RowMouseOver"; } function RowMouseOut(sender, eventArgs) { $get(eventArgs.get_id()).className = "RowMouseOut"; } function ColumnMouseOver(sender, eventArgs) { eventArgs.get_gridColumn().get_element().className = "HeaderMouseOver"; } function ColumnMouseOut(sender, eventArgs) { eventArgs.get_gridColumn().get_element().className = "HeaderMouseOut"; } </script>
After these steps have been performed, when the user hovers with the mouse over the control, the styles mentioned above will be applied, as shown in the following screenshot:
Figure 1: RadGrid row styles applied on hover
