# 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**

```C#
Employee e = this.radGridView.SelectedItem as Employee;
```

```VB.NET
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**

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

```VB.NET
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)

> tip 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**

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

```VB.NET
Me.radGridView.CurrentItem = CType(Me.radGridView.ItemsSource, ObservableCollection(Of Employee)).First()
```

> tip 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**

```C#
this.radGridView.SelectedItems.Add(itemToSelect);
this.radGridView.SelectedItems.Remove(itemToDeselect);
```

```VB.NET
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.

> tip 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](https://github.com/telerik/xaml-sdk/tree/master/GridView/BindingSelectedItemsFromViewModel).

## 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**

```C#
this.radGridView.SelectedCells.Add(new GridViewCellInfo(item, column, this.radGridView));
this.radGridView.SelectedCells.Remove(new GridViewCellInfo(item, column, this.radGridView));
```

```VB.NET
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**

```C#
this.radGridView.Select(itemsToSelect);
```

```VB.NET
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**

```C#
this.radGridView.SelectAll();
```

```VB.NET
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**

```C#
this.radGridView.SelectedItem = null;
```

```VB.NET
Me.radGridView.SelectedItem = Nothing
```

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

**Example 9: Calling SelectedItem's Clear method**

```C#
this.radGridView.SelectedItems.Clear();
```

```VB.NET
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**

```C#

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

```VB.NET

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

> caption The selected region

![Telerik UI for WPF RadGridView showing a single cell region selected programmatically](https://www.telerik.com/products/wpf/documentation/assets/2385edcc2a4d92a9b51ffa7bff618f70/programmatic-selection-select-single-region.jpg)

**Example 11: Selecting multiple cell regions**

```C#

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

```VB.NET

Me.radGridView.SelectCellRegion(New List(Of CellRegion)() From {
	New CellRegion(0, 0, 2, 2),
	New CellRegion(2, 3, 1, 1)
})
```

> caption The selected regions

![Telerik UI for WPF RadGridView showing multiple cell regions selected programmatically](https://www.telerik.com/products/wpf/documentation/assets/49d375dc25882157a7a49dfc6493ac56/programmatic-selection-select-multiple-regions.jpg)

## Deselecting cell regions

You can deselect cell regions by using RadGridView's **UnselectCellRegion** method similarly to using [SelectCellRegion](https://www.telerik.com/products/wpf/documentation/controls/radgridview/selection/programmatic-selection#selecting-cell-regions).

## 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

* [Basic Selection](https://www.telerik.com/products/wpf/documentation/controls/radgridview/selection/basics)

* [Multiple Selection](https://www.telerik.com/products/wpf/documentation/controls/radgridview/selection/multiple-selection)

* [Programmatic Filtering](https://www.telerik.com/products/wpf/documentation/controls/radgridview/filtering/programmatic)

* [Programmatic Sorting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/sorting/programmatic)
