Back to Devexpress

Cell Selection

wpf-7556-controls-and-libraries-data-grid-focus-navigation-selection-multiple-cell-selection.md

latest10.9 KB
Original Source

Cell Selection

  • Jan 08, 2024
  • 4 minutes to read

The GridControl allows you to select cells and their ranges.

Run Demo: Multiple Cell Selection

Enable Multiple Cell Selection

To enable multiple cell selection:

  1. Check that the DataViewBase.NavigationStyle property is set to GridViewNavigationStyle.Cell (default value).
  2. Set the DataControlBase.SelectionMode property to MultiSelectMode.Cell.

In this mode, the GridControl considers the row with the focused cell as a focused row.

PropertyDescription
DataControlBase.CurrentItemReturns the latest selected object (focused row).
DataControlBase.SelectedItemReturns the first selected object (the first item in the DataControlBase.SelectedItems collection).
DataControlBase.SelectedItemsContains the row’s data items that contain the selected cells.

Refer to the Select Multiple Cells topic for information on how end users can select cells in the multiple cell selection mode.

Selection Availability

Handle the TableView.CanSelectCell / TreeListView.CanSelectCell or TableView.CanUnselectCell / TreeListView.CanUnselectCell event to control whether users can select or unselect cells:

xaml
<dxg:GridControl x:Name="grid"
                 SelectionMode="Cell">
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view"
                       CanSelectCell="View_CanSelectCell"
                       CanUnselectCell="View_CanUnselectCell">
        </dxg:TableView>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
void View_CanSelectCell(object sender, CanSelectCellEventArgs e) {
    e.CanSelectCell = e.Column.FieldName != "Visits";
}
void View_CanUnselectCell(object sender, CanUnselectCellEventArgs e) {
    e.CanUnselectCell = e.Column.FieldName != "Birthday";
}
vb
Class SurroundingClass
    Private Sub View_CanSelectCell(ByVal sender As Object, ByVal e As CanSelectCellEventArgs)
        e.CanSelectCell = e.Column.FieldName <> "Visits"
    End Sub

    Private Sub View_CanUnselectCell(ByVal sender As Object, ByVal e As CanUnselectCellEventArgs)
        e.CanUnselectCell = e.Column.FieldName <> "Birthday"
    End Sub
End Class

Select Cells

APIDescription
DataControlBase.BeginSelectionPrevents selection updates until the DataControlBase.EndSelection method is called.
DataControlBase.EndSelectionEnables selection updates previously suppressed by a DataControlBase.BeginSelection method call. Forces a selection update.
TableView.SelectCell / TreeListView.SelectCellSelects the cell identified by its row handle and column.
TableView.SelectCells / TreeListView.SelectCellsSelects multiple cells.
TableView.UnselectCell / TreeListView.UnselectCellUnselects the specified cell.
TableView.UnselectCells / TreeListView.UnselectCellsUnselects multiple cells.
TableView.GetSelectedCells / TreeListView.GetSelectedCellsReturns a collection of selected cells in the TableView / TreeListView.
GridControl.GetSelectedRowHandlesReturns the handles of the rows that contain the selected cells.
DataControlBase.SelectedItemChangedThis event occurs after the GridControl‘s primary selected item is changed.
GridControl.SelectionChanged / TreeListControlBase.SelectionChangedThis event occurs after the GridControl‘s / TreeListControl‘s selection is changed.

The following code sample demonstrates how to iterate through cells and select cells that contain the specified value:

csharp
void Button_Click(object sender, RoutedEventArgs e) {
    SelectCellInColumn(8);
}
public void SelectCellInColumn(object value) {
    grid.BeginSelection();
    grid.UnselectAll();
    for (int index = 0; index < grid.VisibleRowCount; index++) {
        var rowHandle = grid.GetRowHandleByVisibleIndex(index);
        var cellValue = grid.GetCellValue(rowHandle, "Visits");
        if (cellValue != null && cellValue.Equals(value))
            view.SelectCell(rowHandle, grid.Columns["Visits"]);
    }
    grid.EndSelection();
}
vb
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    SelectCellInColumn(8)
End Sub

Public Sub SelectCellInColumn(ByVal value As Object)
    grid.BeginSelection()
    grid.UnselectAll()
    For index As Integer = 0 To grid.VisibleRowCount - 1
        Dim rowHandle = grid.GetRowHandleByVisibleIndex(index)
        Dim cellValue = grid.GetCellValue(rowHandle, "Visits")
        If cellValue IsNot Nothing AndAlso cellValue.Equals(value) Then view.SelectCell(rowHandle, grid.Columns("Visits"))
    Next
    grid.EndSelection()
End Sub

Obtain Selected Cells

Use the TableView.GetSelectedCells / TreeListView.GetSelectedCells method to obtain selected cells. This method returns an array of GridCell objects that contain cell coordinates (row and column).

This code sample demonstrates how to obtain display text from selected cells:

csharp
public IEnumerable<string> GetCellsDisplayText() {
    var list = new List<string>();
    foreach (var cell in view.GetSelectedCells())
        list.Add(grid.GetCellDisplayText(cell.RowHandle, cell.Column));
    return list;
}
vb
Public Function GetCellsDisplayText() As IEnumerable(Of String)
    Dim list = New List(Of String)()

    For Each cell In view.GetSelectedCells()
        list.Add(grid.GetCellDisplayText(cell.RowHandle, cell.Column))
    Next

    Return list
End Function

Change Selected Cell Appearance

You can change a cell’s appearance according to its selection state. To set a cell’s appearance for the entire GridControl, use the DataViewBase.CellStyle property; for individual columns, use ColumnBase.CellStyle.

Use the LightweightCellEditor.SelectionState property to determine the cell state:

NameDescription
NoneA cell is not focused and is not selected.
FocusedA cell is focused but is not selected.
SelectedA cell is selected but is not focused.
FocusedAndSelectedA cell is focused and selected.
HighlightedA cell is highlighted.
CellMergeA cell is not focused and is not selected, and the TableView.AllowCellMerge property is set to true.

The following code sample changes the background color of cells based on their states:

View Example: Change the Appearance of Focused and Selected Cells

xaml
<dxg:GridColumn FieldName="Visits" IsSmart="True">
    <dxg:GridColumn.CellStyle>
        <Style TargetType="dxg:LightweightCellEditor">
            <Style.Triggers>
                <Trigger Property="SelectionState" Value="Selected">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
                <Trigger Property="SelectionState" Value="FocusedAndSelected">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </dxg:GridColumn.CellStyle>
</dxg:GridColumn>

See Also

Row Selection

Common Selection Features