The Grid Control supports single and multiple row/card selection in the GridView, BandedGridView, AdvBandedGridView, LayoutView, CardView and WinExplorerView.
The GridView and BandedGridView additionally provide the multiple cell selection mode. This topic covers information on these modes and related API.

Online Video

Single Row/Card Selection Mode
This is the default selection mode for all Views in which a row/card can only be focused (selected) one at a time.

The table below lists members that can be used to focus (select) records, access the currently focused record and respond to the focus movement.

Multiple Row/Card Selection Mode
In this mode, multiple rows/cards can be selected at one time. By selecting rows, you visually mark them. You can then perform various operations on selected rows (copy to the Clipboard (as text), delete, drag, etc.).
By default, selected rows are painted using the same appearance settings as for the focused row, although it is possible to use different appearance settings to paint selected and focused rows. For the difference between focus and selection, see the Focused Row vs Selected Row subsection below.
To enable multiple row/card selection in all Views, set the ColumnViewOptionsSelection.MultiSelect property (accessible from the View's ColumnView.OptionsSelection object) to true.
The GridView provides the additional GridOptionsSelection.MultiSelectMode property, with which you can select between two multiple row selection modes: GridMultiSelectMode.RowSelect (default) and GridMultiSelectMode.CheckBoxRowSelect.
In GridMultiSelectMode.RowSelect mode, an end-user can select rows with the keyboard and mouse.

In GridMultiSelectMode.CheckBoxRowSelect mode, the GridView displays an additional 'Check' column containing check boxes in each row. An end-user can toggle check boxes to change row selection. When using the Check column, you can bind row selection states to a data source field.

See the Multiple Row Selection via Built-In Check Column and Selection Binding topic to learn more.
Important
Data Grid cells can have interactive editors (for instance, check boxes or ButtonEdit buttons). In this case, if the Grid allows end-users to select multiple rows/cells, clicking such elements activates the cell but since the editor does not yet exist, it does not raise events (e.g., "Click" or "CheckedChanged"). To trigger these event, a user must click the element for the second time. To change this default behavior, set the ColumnViewOptionsBehavior.EditorShowMode property to the EditorShowMode.MouseDown value.

Focused Row vs Selected Row
The focused row/card is the row that accepts input from an end-user. There can only be one focused row in a View, regardless of selection mode.
Compared to row focusing, row selection is only a visual feature designated for highlighting certain rows.
In multiple row/card selection mode, when an end-user focuses a new row with either the Arrow keyboard keys or using the mouse, then that row is selected (highlighted). When you focus a certain row in code (e.g., with the ColumnView.FocusedRowHandle property), this row is not automatically selected (highlighted).
You can select/deselect the focused row in code when required. An end-user can also toggle the selected state for the focused row using the mouse and keyboard, as shown in the End-User Capabilities - Select Grid Rows and Cards topic.
The following images demonstrate the selected and deselected states for a focused row.
Focused Row is selected

|
The ColumnView.GetSelectedRows method will return three row handles, including the focused row handle.
|
Focused Row is deselected

|
The ColumnView.GetSelectedRows method will return two handles that correspond to selected rows. The focused row handle will not be included in the method's resulting array.
|
The table below lists members used in working with selection.
Each time the selection is changed in multiple selection mode, the ColumnView.SelectionChanged event is generated. You can handle it to perform specific actions. To respond to row focus moving from one row to another (even when multiple selection mode is not used), you can handle the ColumnView.FocusedRowChanged event.
When you make multiple successive calls to the methods that change selection, it is best to wrap such calls in calls to the BaseView.BeginSelection/BaseView.EndSelection methods. This prevents the ColumnView.SelectionChanged event from being fired repeatedly, and thus improves your application's performance. See the Batch Modifications Overview topic for more information.

Multiple Cell Selection Mode
This selection mode, supported by GridView and BandedGridView, allows an end-user to select contiguous blocks of cells as well as individual cells within different rows. Multiple rows can also be selected in their entirety, for instance, by clicking the Row Indicator Panel.

To enable multiple cell selection mode, set the ColumnViewOptionsSelection.MultiSelect property to true and the GridOptionsSelection.MultiSelectMode property to GridMultiSelectMode.CellSelect.
Similar to the previous selection mode, in this mode, the focused row can be in the selected and deselected state.
The following table lists the methods to select/unselect cells.
Also, see the list of methods for working with the selection provided in the previous section. When applied in multiple cell selection mode, these methods will affect all cells that belong to the specified row(s).

Common Methods to Work with Selection and Rows

End-User Capabilities
An end-user can select rows/cards and cells with a mouse and keyboard. Please refer to the End-user Capabilities.Selecting Rows topic for more details.

Appearance Settings
By default, selected and focused rows are painted using the same appearance settings. To customize them, use the following properties that are available using a View's Appearance object (GridView.Appearance, CardView.Appearance, etc.).
In specific instances, you may need to prevent the focused row from being highlighted in the GridView or its descendant. To do this, set the GridOptionsSelection.EnableAppearanceFocusedRow property to false. The coloring of a focused cell can be disabled using the GridOptionsSelection.EnableAppearanceFocusedCell option.
To prevent selected rows from being highlighted in a specific manner when the grid control is not focused, use the GridOptionsSelection.EnableAppearanceHideSelection property.

Example - Obtain the Selected Rows
This example demonstrates how to obtain selected rows, and then change values of their "Discounted" column cells.
If Grid data is sorted or filtered, changes made to one Grid row can make other rows change their order and, as a result, their row handles. For that reason you cannot access selected rows by their handles and process these rows right away. Instead, pass row handles retrieved by ColumnView.GetSelectedRows method to the ColumnView.GetDataRow method. This allows you to access underlying data source objects (in this example, DataRows), save them to an array, and safely change their values afterwards.
Each row modification forces the Grid View to update itself. Enclose your code with the BaseView.BeginUpdate and BaseView.EndUpdate method calls to avoid excessive updates. Refer to the Batch Modifications Overview document for details.
C# |
ArrayList rows = new ArrayList();
Int32[] selectedRowHandles = gridView1.GetSelectedRows();
for (int i = 0; i < selectedRowHandles.Length; i++) {
int selectedRowHandle = selectedRowHandles[i];
if (selectedRowHandle >= 0)
rows.Add(gridView1.GetDataRow(selectedRowHandle));
}
try {
gridView1.BeginUpdate();
for (int i = 0; i < rows.Count; i++) {
DataRow row = rows[i] as DataRow;
row["Discontinued"] = true;
}
}
finally {
gridView1.EndUpdate();
}
|
VB |
Dim Rows As New ArrayList()
Dim selectedRowHandles As Int32() = GridView1.GetSelectedRows()
Dim I As Integer
For I = 0 To selectedRowHandles.Length - 1
Dim selectedRowHandle As Int32 = selectedRowHandles(I)
If (selectedRowHandle >= 0) Then
Rows.Add(GridView1.GetDataRow(selectedRowHandle))
End If
Next
Try
GridView1.BeginUpdate()
For I = 0 To Rows.Count - 1
Dim Row As DataRow = CType(Rows(I), DataRow)
Row("Discontinued") = True
Next
Finally
GridView1.EndUpdate()
End Try
|

Example - Highlight the Entire Focused Row
This example shows how to paint the focused cell using the same appearance as the entire focused row:

C# |
gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
|
VB |
gridView1.OptionsBehavior.Editable = False
gridView1.OptionsSelection.EnableAppearanceFocusedCell = False
gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus
|