Multiple Cell Selection
- 4 minutes to read
In this mode, an end-user can select contiguous blocks of cells and individual cells within different data rows by clicking them, while holding the SHIFT or CTRL key down. It is enabled if the DataControlBase.SelectionMode property is set to MultiSelectMode.Cell and the DataViewBase.NavigationStyle property is set to GridViewNavigationStyle.Cell.
The methods which can be used to select/unselect cells:
Method | Description |
---|---|
TableView.SelectCell | Selects the specified cell. |
TableView.SelectCells | Selects multiple cells. |
TableView.UnselectCell | Unselects the specified cell. |
TableView.UnselectCells | Unselects multiple cells. |
For information on how to obtain selected cells, see Obtaining Selected Rows and Cells.
Selection Availability
You can dynamically control selection availability and prevent end-users from selecting or unselecting particular cells.
Handle the TableView.CanSelectCell (TreeListView.CanSelectCell) or TableView.CanUnselectCell (TreeListView.CanUnselectCell) event to dynamically allow or prevent any cell selection.
Example: How to Change the Appearance of Selected Cells
You should implement your own CellStyle. In this CellStyle, implement style triggers for the SelectionState property. Four values can be assigned to this property: None, Focused, Selected, FocusedAndSelected.
Tip
GitHub Example: How to change selected cells’ appearance when GridControl’s multi-cell selection is enabled
<Window x:Class="DXGrid_ChangeRowAppearance.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="300" Width="505"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys">
<Window.Resources>
<Style x:Key="SelectionStateCellStyle" TargetType="dxg:LightweightCellEditor"
BasedOn="{StaticResource ResourceKey={dxgt:GridRowThemeKey ResourceKey=LightweightCellStyle}}">
<Style.Triggers>
<Trigger Property="SelectionState" Value="Selected">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="SelectionState" Value="Focused">
<Setter Property="Background" Value="Green"/>
<Setter Property="TextBlock.Foreground" Value="Pink"/>
</Trigger>
<Trigger Property="SelectionState" Value="FocusedAndSelected">
<Setter Property="Background" Value="Gray"/>
<Setter Property="TextBlock.Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FocusedRowStyle" TargetType="dxg:RowControl">
<Style.Triggers>
<Trigger Property="dxg:GridViewBase.IsFocusedRow" Value="True">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" SelectionMode="Cell">
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True"
CellStyle="{StaticResource SelectionStateCellStyle}"
ShowGroupPanel="False" AllowGrouping="False"
UseLightweightTemplates="All">
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
Highlight Cells On Hover
Set the TableView.HighlightItemOnHover/TreeListView.HighlightItemOnHover property to true to enable highlighting cells when hovering:
<dxg:GridControl SelectionMode="Cell">
<dxg:GridControl.View>
<dxg:TableView HighlightItemOnHover="True" />
</dxg:GridControl.View>
<!-- ... --->
</dxg:GridControl>
Limitations
Bands do not support multiple cell selection.