Row Selection
- 5 minutes to read
The GridControl allows you to select rows, nodes, and cards.
Enable Multiple Row Selection
To enable the multiple row, card, or node selection:
- Check that the DataViewBase.NavigationStyle property is not set to GridViewNavigationStyle.None;
- 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.SelectRange | Adds multiple rows/cards/nodes to the selection. |
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. |
GridViewBase.GetSelectedRows / TreeListView.GetSelectedRows | Gets selected rows. |
DataControlBase.GetSelectedRowHandles | Returns the handles of the selected rows. |
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‘s / 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();
}
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
When you select service rows (Auto Filter Row, New Item Row), the GridControl does not raise the GridControl.SelectionChanged / TreeListControlBase.SelectionChanged and SelectedItemChanged events. The GridControl / TreeListControl‘s selection is not reset in this case.
When you work with the master-detail GridControl, the DataControlBase.SelectedItems collection contains items only from the master GridControl. To obtain selected detail records in a DataControlDetailDescriptor, use the detail GridControl‘s DataControlBase.GetSelectedRowHandles method. Next, pass row handles to the GridControl.GetRow method to obtain row data items.
When you filter data, the GridControl resets its selection, then applies a filter. To avoid this behavior, save and restore the DataControlBase.SelectedItems collection.
When the GridControl works in the Server Mode or with Virtual Sources, the DataControlBase.SelectedItems collection returns an empty list. Use the DataControlBase.GetSelectedRowHandles and DataControlBase.GetRow / DataControlBase.GetRowAsync methods to obtain row handles and data items.