Back to Devexpress

Row Selection

wpf-7359-controls-and-libraries-data-grid-focus-navigation-selection-multiple-row-selection.md

latest14.2 KB
Original Source

Row Selection

  • Jul 28, 2025
  • 5 minutes to read

The GridControl allows you to select rows, nodes, and cards.

Run Demo: Multiple Row Selection

View Example: WPF Grid Control - Select All Group Subitems When a Group Row is Clicked

Enable Multiple Row Selection

To enable the multiple row, card, or node selection:

  1. Check that the DataViewBase.NavigationStyle property is not set to GridViewNavigationStyle.None;
  2. Set the DataControlBase.SelectionMode property to MultiSelectMode.Row or MultiSelectMode.MultipleRow.

The DataControlBase.CurrentItem property returns the focused data item. The DataControlBase.SelectedItem property returns the item that was selected first (first item of the DataControlBase.SelectedItems collection). The focused row, card, or node may not be selected.

The DataControlBase.SelectedItems collection is populated with items in the same order rows/nodes are selected. To get selected nodes ordered by visible indexes, use the TreeListControlBase.GetSelectedNodes method. To get row handles of selected items ordered by visible indexes, use the DataControlBase.GetSelectedRowHandles method.

Tip

The Row and MultipleRow modes support the Selector Column.

Refer to the Select Multiple Rows topic for information on how end users can select rows in the multiple row selection mode.

Selection Availability

Handle the DataViewBase.CanSelectRow or DataViewBase.CanUnselectRow event to control whether end users can select or unselect rows, cards, or nodes. The following code sample demonstrates how to exclude rows with the specified cell values from end-user selection and then change the appearance of these rows:

xaml
<dxg:TableView x:Name="view" NavigationStyle="Row" CanSelectRow="View_CanSelectRow" >
    <dxg:TableView.FormatConditions>
        <dxg:FormatCondition Expression="[Visits] >= 8" IsEnabled="True" >
            <dxg:Format Background="#FFB2B2B2"/>
        </dxg:FormatCondition>
    </dxg:TableView.FormatConditions>
</dxg:TableView>
csharp
void View_CanSelectRow(object sender, CanSelectRowEventArgs e) {
    e.CanSelectRow = (Convert.ToDouble(grid.GetCellValue(e.RowHandle, "Visits")) < 8);
}
vb
Private Sub View_CanSelectRow(ByVal sender As Object, ByVal e As CanSelectRowEventArgs)
    e.CanSelectRow = (Convert.ToDouble(grid.GetCellValue(e.RowHandle, "Visits")) < 8)
End Sub

Select Rows

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.
DataControlBase.SelectAllSelects all rows/cards/nodes within a View.
DataControlBase.UnselectAllUnselects any selected rows/cards/nodes within a View.
DataControlBase.SelectItemSelects the specified row/card/node.
DataControlBase.UnselectItemUnselects the specified row/card/node.
DataControlBase.SelectRangeAdds multiple rows/cards/nodes to the selection.
DataControlBase.SelectedItemGet or sets the focused data item (if row multi-selection is disabled); or the item that was selected first (if row multi-selection is enabled).
DataControlBase.SelectedItemsGets or sets data objects that correspond to the selected rows/nodes within a View.
GridViewBase.GetSelectedRows / TreeListView.GetSelectedRowsGets selected rows.
DataControlBase.GetSelectedRowHandlesReturns the handles of the selected rows.
TreeListControlBase.GetSelectedNodesReturns selected nodes.
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 rows and select rows that contain the specified value:

View Example: Select Rows that Contain the Specified Value

csharp
void Button_Click(object sender, RoutedEventArgs e) {
    SelectRowInColumn(6);
}

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

Public Sub SelectRowInColumn(ByVal value As Object)
    grid.BeginSelection()
    grid.UnselectAll()

    For index As Integer = 0 To grid.VisibleRowCount - 1
        Dim rowHandle As Integer = grid.GetRowHandleByVisibleIndex(index)
        Dim cellValue = grid.GetCellValue(rowHandle, "Visits")
        If cellValue IsNot Nothing AndAlso cellValue.Equals(value) Then grid.SelectItem(rowHandle)
    Next

    grid.EndSelection()
End Sub

Obtain Selected Rows

To obtain selected rows/nodes or their handles, use DataControlBase.SelectedItems or DataControlBase.GetSelectedRowHandles respectively.

The following code sample shows how to process selected rows, and unselect rows where the number of visits is less than 10:

csharp
void ProcessSelectedRows(GridControl gridControl, GridViewBase view) {
    gridControl.BeginSelection();
    foreach (int rowHandle in gridControl.GetSelectedRowHandles()) {
        if (Convert.ToDouble(gridControl.GetCellValue(rowHandle, "Visits")) < 10)
            gridControl.UnselectItem(rowHandle);
    }
    gridControl.EndSelection();
}
vb
Private Sub ProcessSelectedRows(ByVal gridControl As GridControl, ByVal view As GridViewBase)
    gridControl.BeginSelection()

    For Each rowHandle As Integer In gridControl.GetSelectedRowHandles()
        If Convert.ToDouble(gridControl.GetCellValue(rowHandle, "Visits")) < 10 Then gridControl.UnselectItem(rowHandle)
    Next

    gridControl.EndSelection()
End Sub

Customize Appearance

To change a row’s appearance according to its selection state, use the TableView.RowStyle / TreeListView.RowStyle property.

Use the RowControl.SelectionState property to determine the cell’s state:

Member NameDescription
NoneA row is not focused and is not selected
FocusedA row is focused but is not selected
SelectedA row is selected but is not focused
HighlightedA row is highlighted
FocusedAndSelectedA row is focused and selected

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

View Example: Change the Appearance of Focused and Selected Rows

xaml
<dxg:TableView x:Name="tableView1">
    <dxg:TableView.RowStyle>
        <Style TargetType="dxg:RowControl">
            <Style.Triggers>
                <Trigger Property="SelectionState" Value="Selected">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
                <Trigger Property="SelectionState" Value="Focused">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </dxg:TableView.RowStyle>
</dxg:TableView>

To disable the appearance of selected rows, set the DataViewBase.EnableSelectedRowAppearance property to false.

Notes and Limitations

See Also

Binding to a Collection of Selected Items

Select Rows and Cells

Common Selection Features