New to Telerik UI for WPF? Download free 30-day trial

Handle Cell\Row Mouse Events

This article will demonstrate how to handle RadGridView's CellLoaded & RowLoaded events to add handlers for the various mouse events to the control's cell and row elements. The CellUnloaded and RowUnloaded events can in turn be used to remove those handlers in order to avoid memory leaks.

You should not directly set properties of the visual elements (GridViewCell, GridViewRow) as this will result in inconsistent behavior due to the control's UI Virtualization mechanism. Instead, you should use the underlying data items as explained in the Style Selectors section.

CellLoaded Event

The CellLoaded event fires anytime a cell appears in the viewport.

The CellLoaded event handler receives two arguments:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • A CellEventArgs object. This object exposes a Cell property - the loaded cell.

Example 1 uses the CellLoaded event to handle the left-click on a GridViewCell and GridViewHeaderCell.

Example 1: Handling CellLoaded

private void RadGridView_CellLoaded(object sender, RowLoadedEventArgs e)   
{   
    if (e.Cell is GridViewHeaderCell) 
    { 
        e.Cell.AddHandler(GridViewHeaderCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnHeaderCellMouseLeftButtonDown), true); 
    } 
    else if (e.Cell is GridViewCell) 
    { 
        e.Cell.AddHandler(GridViewCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnCellMouseLeftButtonDown), true); 
    } 
} 
 
private void OnHeaderCellMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs) 
{ 
    MessageBox.Show("Left-clicked header cell!"); 
} 
 
private void OnCellMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    MessageBox.Show("Left-clicked cell!"); 
} 

CellUnloaded event

The CellUnloaded event occurs anytime a cell disappears from the viewport.

The CellUnloaded event handler receives two arguments:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • A CellEventArgs object. This object exposes a Cell property - the unloaded cell.

A common scenario in which the CellUnloaded can be used is to remove the handler(s) added in the CellLoaded event.

Example 2: Handling CellUnloaded

private void RadGridView_CellUnloaded(object sender, CellEventArgs e) 
{ 
    e.Cell.RemoveHandler(GridViewHeaderCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnHeaderCellMouseLeftButtonDown)); 
    e.Cell.RemoveHandler(GridViewHeaderCell.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnCellMouseLeftButtonDown)); 
} 

RowLoaded Event

The RowLoaded event fires anytime a row appears in the viewport.

The RowLoaded event handler receives two arguments:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • A RowLoadedEventArgs object. This object has the following properties:

    • DataElement: Gets the data element (data context) for the row being loaded.

    • GridViewDataControl: The GridViewDataControl control holding the row being loaded.

    • Row: The row being loaded.

Example 1 uses the RowLoaded event to handle the right-click on a GridViewRow.

Example 3: Handling RowLoaded\RowUnloaded

private void RadGridView_RowLoaded(object sender, RowLoadedEventArgs e)   
{   
    if (e.Row is GridViewRow && !(e.Row is GridViewNewRow))   
    {   
        e.Row.AddHandler(GridViewRow.MouseRightButtonDownEvent, new MouseButtonEventHandler(OnMouseRightButtonDown), true); 
    }   
} 
 
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs) 
{ 
    var row = sender as GridViewRow; 
    var club = row.DataContext as Club; 
    MessageBox.Show($"Right-clicked {club.Name}!"); 
} 

RowUnloaded event

The RowUnloaded event occurs anytime a row disappears from the viewport.

The RowUnloaded event handler receives two arguments:

  • The sender argument contains the RadGridView. This argument is of type object, but can be cast to the RadGridView type.

  • A RowLoadedEventArgs object. This object has the following properties:

    • DataElement: Gets the data element (data context) for the row being unloaded.

    • GridViewDataControl: The gridview control holding the row being unloaded.

    • Row: The row being unloaded.

A common scenario in which the RowUnloaded can be used is to remove the handler(s) added in the RowLoaded event.

Example 4: Handling CellUnloaded

private void RadGridView_RowUnloaded(object sender, RowUnloadedEventArgs e) 
{ 
    e.Row.RemoveHandler(GridViewRow.MouseRightButtonDownEvent, new MouseButtonEventHandler(OnMouseRightButtonDown)); 
}