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

Programmatic Selection

Besides the built-in selection functionality, you are able to use a programmatic approach to select the data in the RadGridView. The RadGridView exposes several properties that allow you to manipulate the selected data.

SelectedItem

To access the data item of the selected row use the SelectedItem property. It changes its value every time when the row selection changes and exposes the object to which the row is bound. You can use it when the SelectionUnit is set to FullRow (default), otherwise it is null.

Example 1: Accessing RadGridView's selected item

Employee e = this.radGridView.SelectedItem as Employee; 
Dim e As Employee = TryCast(Me.radGridView.SelectedItem, Employee) 

You can also set the SelectedItem to an item in the RadGridView's data source and it will get automatically selected. For example, if you want on a certain action the first item in your RadGridView to be selected, you can do the following.

Example 2: Setting the SelectedItem to an item of the ItemsSource

this.radGridView.SelectedItem = ((ObservableCollection<Employee>)this.radGridView.ItemsSource).First(); 
Me.radGridView.SelectedItem = CType(Me.radGridView.ItemsSource, ObservableCollection(Of Employee)).First() 

When choosing this approach, you need to make sure that the data is already loaded. (e.g. you can use the above code in the DataLoaded event of the RadGridView)

You can also make another control like ListBox or ComboBox to pass the SelectedItem to the RadGridView.

CurrentItem

The CurrentItem property of the RadGridView corresponds to the CurrentItem of the bound ICollectionView. It may or may not coincide with the selected row. However, in most of the cases these two rows are one and the same.

You can also set the CurrentItem to an item in the RadGridView's data source and it will get automatically selected. For example, if you want on a certain action the first item in your RadGridView to be current you can do the following.

Example 3: Setting the CurrentItem

this.radGridView.CurrentItem = ((ObservableCollection<Employee>)this.radGridView.ItemsSource).First(); 
Me.radGridView.CurrentItem = CType(Me.radGridView.ItemsSource, ObservableCollection(Of Employee)).First() 

You can also make another control like ListBox or ComboBox to pass the CurrentItem to the RadGridView.

Prior to R2 2010 version, the current item was synchronized with the selected item. As a result, the first row of the GridView was selected initially. To prevent this, you would simply need to set the IsSynchronizedWithCurrentItem property of RadGridView to False. In R2 2010 version, the IsSynchronizedWithCurrentItem is null by default - SelectedItem is synchronized with the CurrentItem only if CollectionView is used as ItemsSource

SelectedItems

The SelectedItems is a collection of data items, which holds the currently selected items. As it is a collection you can get, add and remove items from it. In this way you can easily manipulate the selection, e.g. selecting and deselecting items on certain criteria. You can use it when the SelectionUnit is set to FullRow (default), otherwise it is null. It will contain more than one item when the SelectionMode is either Multiple or Extended.

If the selection mode is set to Single it will always hold at most one item.

Example 4: Adding/deselecting an item

this.radGridView.SelectedItems.Add(itemToSelect); 
this.radGridView.SelectedItems.Remove(itemToDeselect); 
Me.radGridView.SelectedItems.Add(itemToSelect) 
Me.radGridView.SelectedItems.Remove(itemToDeselect) 

If you add more than one item in the SelectedItems while the selection mode is single, the selection will disappear. Consider working with the collection only when you are using Multiple or Extended selection mode.

You can add or remove items to the SelectedItems collection, but cannot change its instance because it is read only.

Find a runnable project showing how to bind the SelectedItems collection of the RadGridView to a property in your ViewModel in the SDK examples GitHub repository.

SelectedCells

Represents a collection of GridViewCellInfo objects which represent the business object and the column of the selected cell/cells. You can work with it when the SelectionUnit is set to Cell, otherwise it will be null.

Example 5: Adding/removing cells from SelectedCells collection

this.radGridView.SelectedCells.Add(new GridViewCellInfo(item, column, this.radGridView)); 
this.radGridView.SelectedCells.Remove(new GridViewCellInfo(item, column, this.radGridView)); 
Me.radGridView.SelectedCells.Add(New GridViewCellInfo(item, column, Me.radGridView)) 
Me.radGridView.SelectedCells.Remove(New GridViewCellInfo(item, column, Me.radGridView)) 

Select Method

You can also select items by using the Select method which accepts a collection of items. This is a lot quicker than calling the SelectedItems.Add() method for each item.

Example 6: Selecting items

this.radGridView.Select(itemsToSelect); 
Me.radGridView.Select(itemsToSelect) 

Selecting All Items

The RadGridView control also exposes a SelectAll() method, which allows you to select all items at once.

Example 7: Selecting all items

this.radGridView.SelectAll(); 
Me.radGridView.SelectAll() 

Deselecting All Items

There are two ways to remove the selection. The first one is to set the SelectedItem property to null.

Example 8: Deselecting by setting SelectedItem to null

this.radGridView.SelectedItem = null; 
Me.radGridView.SelectedItem = Nothing 

The second one is to call the Clear() method of the SelectedItems collection.

Example 9: Calling SelectedItem's Clear method

this.radGridView.SelectedItems.Clear(); 
Me.radGridView.SelectedItems.Clear() 

Selecting Cell Regions

As of R3 2016 RadGridView exposes a SelectCellRegion method which allows you to select specific region(s) of cells. Its overloads allow you to pass as a parameter either a single CellRegion instance or a collection of cell regions. The CellRegion class has a single constructor through which you can set the following internal properties:

  • Left: The horizontal offset for the top-left cell.

  • Top: The vertical offset for the top-left cell.

  • Width: The number of cells to select horizontally.

  • Height: The number of cells to select vertically.

Example 10: Selecting а single cell region

this.radGridView.SelectCellRegion(new CellRegion(0, 0, 2, 2)); 
Me.radGridView.SelectCellRegion(New CellRegion(0, 0, 2, 2)) 

Figure 1: The selected region

Telerik WPF DataGrid programmatic-selection-select-single-region

Example 11: Selecting multiple cell regions

this.radGridView.SelectCellRegion(new List<CellRegion>() 
{ 
    new CellRegion(0, 0, 2, 2), 
    new CellRegion(2, 3, 1, 1) 
}); 
Me.radGridView.SelectCellRegion(New List(Of CellRegion)() From { 
    New CellRegion(0, 0, 2, 2), 
    New CellRegion(2, 3, 1, 1) 
}) 

Figure 2: The selected regions

Telerik WPF DataGrid  programmatic-selection-select-multiple-regions

Deselecting cell regions

You can deselect cell regions by using RadGridView's UnselectCellRegion method similarly to using SelectCellRegion.

Selecting range of items

To enable this functionality, the SelectionMode needs to be set either to Multiple, or to Extended.

RadGridView exposes a mechanism for a fast programmatic selection of a range of items. The method for utilizing it is the SelectItemsRange. It is available with the 2017.3.1120 latest internal build and the official version that is going to support it is R1 2018. The method exposes two overloads:

  • SelectItemsRange(int startIndex, int endIndex): Selects items in a given range by specified indexes.

  • SelectItemsRange(object startItem, object endItem): Selects items in a given range by specified object instances.

Deselecting items when using the SelectItemsRange needs to be performed through removing items from the SelectedItems collection of RadGridView

See Also

In this article