Row Selection

  • 5 minutes to read

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

TIP

Demo: Multiple Row Selection

Requires a WPF Subscription. Download

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:

<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>
void View_CanSelectRow(object sender, CanSelectRowEventArgs e) {
    e.CanSelectRow = (Convert.ToDouble(grid.GetCellValue(e.RowHandle, "Visits")) < 8);
}

Select Rows

API Description
DataControlBase.BeginSelection Prevents selection updates until the DataControlBase.EndSelection method is called.
DataControlBase.EndSelection Enables selection updates previously suppressed by a DataControlBase.BeginSelection method call. Forces a selection update.
DataControlBase.SelectAll Selects all rows/cards/nodes within a View.
DataControlBase.UnselectAll Unselects any selected rows/cards/nodes within a View.
DataControlBase.SelectItem Selects the specified row/card/node.
DataControlBase.UnselectItem Unselects the specified row/card/node.
DataControlBase.SelectedItem Get 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.SelectedItems Gets or sets data objects that correspond to the selected rows/nodes within a View.
DataControlBase.GetSelectedRowHandles Returns the handles of the selected rows.
DataControlBase.SelectRange Adds multiple rows/cards/nodes to the selection.
TreeListControlBase.GetSelectedNodes Returns selected nodes.
DataControlBase.SelectedItemChanged This event occurs after the GridControl's primary selected item is changed.
GridControl.SelectionChanged / TreeListControlBase.SelectionChanged This event occurs after the GridControl / TreeListControl 's selection is changed.

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

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();
}
NOTE

A complete sample project is available at How to: Select Rows that Contain The Specified Value.

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:

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();
}

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 Name Description
None A row is not focused and is not selected
Focused A row is focused but is not selected
Selected A row is selected but is not focused
Highlighted A row is highlighted
FocusedAndSelected A row is focused and selected

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

<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