# Selecting Rows and Cells Programmatically

This article describes the methods to select rows and cells through code.

## Selecting a single row

To select a single row programmatically:

* You can set its **IsSelected** property to `true`:

```C#
radGridView1.Rows[2].IsSelected = true;
```

```VB.NET
RadGridView1.Rows(2).IsSelected = True
```

* You can make the row current:

```C#
radGridView1.Rows[2].IsCurrent = true;
```

```VB.NET
RadGridView1.Rows(2).IsCurrent = True
```

Both ways to select a single row result in adding this row into the RadGridView.**SelectedRows** collection.

![WinForms RadGridView Select Row Programmatically](https://www.telerik.com/products/winforms/documentation/assets/2f7753ce07d85993386ebc7eb5f390fa/gridview-selection-selecting-rows-and-cells-programmatically001.png)

## Selecting Multiple Rows

To select multiple rows programmatically, set their **IsSelected** property to `true`:

```C#
radGridView1.ClearSelection();
radGridView1.MultiSelect = true;
radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.FullRowSelect;
radGridView1.Rows[0].IsSelected = true;
radGridView1.Rows[4].IsSelected = true;
radGridView1.Rows[6].IsSelected = true;
radGridView1.Rows[9].IsSelected = true;
```

```VB.NET
RadGridView1.ClearSelection()
RadGridView1.MultiSelect = True
RadGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.FullRowSelect
RadGridView1.Rows(0).IsSelected = True
RadGridView1.Rows(4).IsSelected = True
RadGridView1.Rows(6).IsSelected = True
RadGridView1.Rows(9).IsSelected = True
```

![WinForms RadGridView Selecting Multiple Rows Programmatically](https://www.telerik.com/products/winforms/documentation/assets/c35b0082beaf16f58f483a98fa908698/gridview-selection-selecting-rows-and-cells-programmatically002.png)

In this scenario all, four rows are added to the **SelectedRows** collection of **RadGridView**. You can access the instances of the selected rows in the **SelectedRows** collection by their index:

```C#
GridViewRowInfo selectedRow = radGridView1.SelectedRows[0];
```

```VB.NET
Dim selectedRow As GridViewRowInfo = RadGridView1.SelectedRows(0)
```

> note The rows are added to the **SelectedRows** collection in the same order as the order in which you have set the selected rows.

## Selecting a Single Cell

You can select cells the same way you select rows – by setting their **IsSelected** property to `true`:

```C#
radGridView1.ClearSelection();
radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;
//here instead of a cell index you can specify the Name of the column as a string
//i.e. radGridView1.Rows[1].Cells[“Column 1”].IsSelected = true;
radGridView1.Rows[1].Cells[3].IsSelected = true;
```

```VB.NET
RadGridView1.ClearSelection()
RadGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect
'here instead of a cell index you can specify the Name of the column as a string
'i.e. radGridView1.Rows(1).Cells(“Column 1”).IsSelected = true
RadGridView1.Rows(1).Cells(3).IsSelected = True
```

Selecting a single cell will result in adding this cell into the RadGridView.**SelectedCells** collection.

![WinForms RadGridView Selecting a single cell Programmatically](https://www.telerik.com/products/winforms/documentation/assets/45495469bc10679863ce46a5baaaea88/gridview-selection-selecting-rows-and-cells-programmatically003.png)

## Selecting Multiple Cells

To select multiple cells programmatically, set the **IsSelected** property of the desired cells to `true`.

```C#
radGridView1.MultiSelect = true;
radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;
radGridView1.Rows[0].Cells[0].IsSelected = true;
radGridView1.Rows[3].Cells[1].IsSelected = true;
radGridView1.Rows[5].Cells[2].IsSelected = true;
radGridView1.Rows[6].Cells[3].IsSelected = true;
```

```VB.NET
RadGridView1.MultiSelect = True
RadGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect
RadGridView1.Rows(0).Cells(0).IsSelected = True
RadGridView1.Rows(3).Cells(1).IsSelected = True
RadGridView1.Rows(5).Cells(2).IsSelected = True
RadGridView1.Rows(6).Cells(3).IsSelected = True
```

![WinForms RadGridView Selecting Multiple Cells Programmatically](https://www.telerik.com/products/winforms/documentation/assets/b56a1bac072ca237531eb9db1aee0bad/gridview-selection-selecting-rows-and-cells-programmatically004.png)

In this scenario, all four cells are added to the **SelectedCells** collection of **RadGridView**. You can access the instances of the selected cells in the **SelectedCells** collection by their index:

```C#
GridViewCellInfo selectedCell = radGridView1.SelectedCells[0];
```

```VB.NET
Dim selectedCell As GridViewCellInfo = RadGridView1.SelectedCells(0)
```

Note that the cells are added to the collection in the same order as the order in which you have set the selected cells.

## BaseGridNavigator's Selection API

**BaseGridNavigator** provides a suitable API for selecting rows and columns programmatically. You can access it through the RadGridView.**GridNavigator** property. The following table lists the available public methods:

| Method                                                 | Description                                                                                                                                         |
| ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| **SelectAll**                                          | Select all rows and cells.                                                                                                                          |
| **ClearSelection**                                     | Clears the selection.                                                                                                                               |
| **BeginSelection**                                     | Begins grid selection.                                                                                                                              |
| **EndSelection**                                       | Ends selection.                                                                                                                                     |
| **Select(GridViewRowInfo row, GridViewColumn column)** | Selects the specified row as current and specified column as current.                                                                               |
| **SelectFirstRow**                                     | Selects the first row as current column in grid. The method returns *true* if the operation is successful.                                          |
| **SelectLastRow**                                      | Selects the last row as current row in grid. The method returns *true* if the operation is successful.                                              |
| **SelectRow(GridViewRowInfo row)**                     | Selects the specified row as current row in grid. The method returns *true* if the operation is successful.                                         |
| **SelectNextRow(int step)**                            | Selects the row at specified distance after the current position as current row in grid. The method returns *true* if the operation is successful.  |
| **SelectPreviousRow(int step)**                        | Selects the row at specified distance before the current position as current row in grid. The method returns *true* if the operation is successful. |

## See Also

* [Basic Selection](https://www.telerik.com/products/winforms/documentation/controls/gridview/features/selection/basic-selection)

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