RadDataGrid provides you with selection functionality, which allows the user to select one or more rows and/or cells from the data displayed by the control.
The RadDataGrid control provides four selection modes, which allow you to manipulate the type of selection. This is controlled by the SelectionMode property which accepts the following values:
- Single: A single unit may be selected (default value).
- Multiple: Multiple units may be selected.
- Extended: Items are added to the selection only by combining the mouse clicks with the Ctrl or Shift keys.
- None: No selection is allowed.
RadDataGrid also provides three selection units, which allow you to manipulate what units are selected when you interact with the grid. This is controlled by the SelectionUnit property which accepts the following values:
- Row: Clicking within the cells will select the whole row (default value).
- Cell: The clicked cell is selected.
RadDataGrid provides several properties to get the data behind the selected items - SelectedItem and SelectedItems.
SelectedItem: A property of type object that gets or sets the currently selected item.
SelectedItems: A property of type ObservableCollection
Depending on the SelectionUnit, the SelectedItem and SelectedItems properties will contain different objects. If the SelectionUnit of the RadDataGrid is Cell, the properties will hold DataGridCellInfo objects, which carry information about the selected cell and the underlying data item. If the SelectionUnit of the RadDataGrid is Row, the properties will hold the business object that represents that row.
Besides the built-in selection functionality, you are able to use a programmatic approach to select the data in the RadDataGrid. Here are the methods that the control exposes:
- SelectItem(object item): Selects the provided data item and adds it in the SelectedItems collection.
- DeselectItem(object item): Removes the selection for the provided data item and removes it from the SelectedItems collection.
- SelectCell(DataGridCellInfo item): Selects the grid cell as defined by the provided cell info.
- DeselectCell(DataGridCellInfo item): Removes the selection for the grid cell defined by the provided cell info.
- SelectAll(): Selects all the items as defined by the SelectionMode and SelectionUnit properties.
- DeselectAll(): Clears the current selected items as defined by the SelectionUnit properties.
The RadDataGrid exposes the SelectionChanged event, which will be raised whenever the selection is changed. The event arguments provide information about the added and removed items.