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.
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:
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 the
None—No selection is allowed.
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:
Row—Clicking within the cells will select the whole row.
Cell—The clicked cell is selected.
The DataGrid provides the following properties for getting the data behind the selected items:
SelectedItem—A property of type
objectthat gets or sets the currently selected item.
SelectedItems—A property of type
ObservableCollection<object>that gets an
ObservableCollectionof the currently selected items.
Depending on the
SelectedItems properties will contain different objects. If the
Cell, the properties will hold
DataGridCellInfo objects, which carry information about the selected cell and the underlying data item. If the
Row, the properties will hold the business object that represents that row.
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 the
DeselectItem(object item)—Removes the selection for the provided data item and removes it from the
DataGridCellInfoitem)—Selects the grid cell as defined by the provided cell information.
DataGridCellInfoitem)—Removes the selection for the grid cell defined by the provided cell information.
SelectAll()—Selects all the items as defined by the
DeselectAll()—Clears the current selected items as defined by the
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.