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
:
radGridView1.Rows[2].IsSelected = true;
RadGridView1.Rows(2).IsSelected = True
- You can make the row current:
radGridView1.Rows[2].IsCurrent = true;
RadGridView1.Rows(2).IsCurrent = True
Both ways to select a single row result in adding this row into the RadGridView.SelectedRows collection.
Selecting Multiple Rows
To select multiple rows programmatically, set their IsSelected property to true
:
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;
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
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:
GridViewRowInfo selectedRow = radGridView1.SelectedRows[0];
Dim selectedRow As GridViewRowInfo = RadGridView1.SelectedRows(0)
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
:
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;
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.
Selecting Multiple Cells
To select multiple cells programmatically, set the IsSelected property of the desired cells to true
.
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;
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
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:
GridViewCellInfo selectedCell = radGridView1.SelectedCells[0];
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. |