Edit this page

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 which allow you to manipulate the selected data. You are able to:

You can download a runnable project showing how to bind the SelectedItems collection of the RadGridView to a property in your ViewModel here, the example is listed as GridView/BindingSelectedItemsFromViewModel.

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.

[C#] Example 1: Accessing RadGridView's selected item

Employee e = this.radGridView.SelectedItem as Employee;

[VB.NET] Example 1: Accessing RadGridView's selected item

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.

[C#] Example 2: Setting the SelectedItem to an item of the ItemsSource

this.radGridView.SelectedItem = ((ObservableCollection<Employee>)this.radGridView.ItemsSource).First();

[VB.NET] Example 2: Setting the SelectedItem to an item of the ItemsSource

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.

[C#] Example 3: Setting the CurrentItem

this.radGridView.CurrentItem = ((ObservableCollection<Employee>)this.radGridView.ItemsSource).First();

[VB.NET] Example 3: Setting the CurrentItem

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.

[C#] Example 4: Adding/deselecting an item

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

[VB.NET] Example 4: Adding/deselecting an item

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.

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.

[C#] 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));

[VB.NET] Example 5: Adding/removing cells from SelectedCells collection

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

Selecting All Items

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

[C#] Example 6: Selecting all items

this.radGridView.SelectAll();

[VB.NET] Example 6: Selecting all items

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.

[C#] Example 7: Deselecting by setting SelectedItem to null

this.radGridView.SelectedItem = null;

[VB.NET] Example 7: Deselecting by setting SelectedItem to null

Me.radGridView.SelectedItem = Nothing

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

[C#] Example 8: Calling SelectedItem's Clear method

this.radGridView.SelectedItems.Clear();

[VB.NET] Example 8: Calling SelectedItem's Clear method

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.

[C#] Example 9: Selecting а single cell region

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

[VB.NET] Example 9: Selecting а single cell region

Me.radGridView.SelectCellRegion(New CellRegion(0, 0, 2, 2))

Figure 1: The selected region

The selected region

[C#] Example 9: Selecting multiple cell regions

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

[VB.NET] Example 9: Selecting multiple cell regions

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

The selected regions

Deselecting cell regions

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

See Also