Selection
The DataGrid provides a selection functionality, which allows the user to select one or more rows and/or cells from the data displayed by the component.
Selection Modes
The DataGrid provides the following selection modes, which allow you to manipulate the type of selection. To control the modes, use the SelectionMode
property, which accepts the following values:
- (Default)
Single
—A single unit may be selected. -
Multiple
—Multiple units can be selected. -
Extended
—Items are added to the selection only by combining the mouse clicks with theCtrl
orShift
keys. -
None
—No selection is allowed.
Selection Units
The DataGrid also provides three selection units, which allow you to manipulate what units are selected when you interact with the grid. To control the selection units, set the SelectionUnit
property, which accepts the following values:
- (Default)
Row
—Clicking within the cells will select the whole row. -
Cell
—The clicked cell is selected.
Selected Items
The DataGrid provides the following properties for getting the data behind the selected items:
SelectedItem
—A property of typeobject
that gets or sets the currently selected item.SelectedItems
—A property of typeObservableCollection<object>
that gets anObservableCollection
of the currently selected items.
Depending on the SelectionUnit
, the SelectedItem
and SelectedItems
properties will contain different objects. If the SelectionUnit
is Cell
, the properties will hold DataGridCellInfo
objects, which carry information about the selected cell and the underlying data item. If the SelectionUnit
is Row
, the properties will hold the business object that represents that row.
Programmatic Selection
Besides the built-in selection functionality, you can use a programmatic approach to select the data in the DataGrid.
The component exposes the following methods for programmatic selection:
-
SelectItem
(object item)—Selects the provided data item and adds it in theSelectedItems
collection. -
DeselectItem
(object item)—Removes the selection for the provided data item and removes it from theSelectedItems
collection. -
SelectCell
(DataGridCellInfo
item)—Selects the grid cell as defined by the provided cell information. -
DeselectCell
(DataGridCellInfo
item)—Removes the selection for the grid cell defined by the provided cell information. -
SelectAll
()—Selects all the items as defined by theSelectionMode
andSelectionUnit
properties. -
DeselectAll
()—Clears the current selected items as defined by theSelectionUnit
properties.
Events
The DataGrid exposes the SelectionChanged
event, which will be raised whenever the selection is changed. The event arguments provide information about the added and removed items.