Ordered Selection Behavior
- 3 minutes to read
Ordered Selection Behavior allows you to track row selection order when multiple selection is enabled.

You can attach this behavior to Data Grid views.
Activate Ordered Selection
Activate multiple row selection. Set the view’s OptionsSelection.MultiSelect to
trueand OptionsSelection.MultiSelectMode toGridMultiSelectMode.RowSelect(orCheckBoxRowSelect).Tip
For more information on multiple row selection, refer to Multiple Row and Cell Selection.
Drop a BehaviorManager onto the form.
- Attach an
OrderedSelectionBehaviorat design time (click Edit Behaviors in theBehaviorManagersmart tag) or in code as follows:
using DevExpress.Utils.Behaviors.Common;
using DevExpress.XtraBars.Ribbon;
using System.Data;
using DevExpress.XtraGrid.Views.Grid;
namespace orderedSelection {
public partial class Form1 : RibbonForm {
public Form1() {
InitializeComponent();
ConfigureGridSelection();
behaviorManager1.Attach<OrderedSelectionBehavior>(gridView);
}
private void ConfigureGridSelection() {
gridView.OptionsSelection.MultiSelect = true;
gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
}
}
}
Key API
API | Description |
|---|---|
| Returns an array of data row objects in the order rows were selected. |
| Returns the row’s selection index. |
Handle Selection Changes
The OrderedSelectionBehaviorEvents.SelectionChanged event fires when the collection of selected row indexes changes. Handle this event to respond to selection changes. For example, the following code snippet populates a ListBox with values from selected rows:
using DevExpress.Utils.Behaviors.Common;
using System.Data;
using System.Collections.Generic;
private void OrderedSelectionBehaviorEvents1_SelectionChanged(object? sender, EventArgs e) {
OrderedSelectionBehavior behavior = behaviorManager1.GetBehavior<OrderedSelectionBehavior>(gridView);
for (int rowHandle = 0; rowHandle < gridView.DataRowCount; rowHandle++) {
object row = gridView.GetRow(rowHandle);
string car = (string)gridView.GetRowCellValue(rowHandle, "Item");
string value = $"{car} - {gridView.GetRowCellValue(rowHandle, "Price")}";
bool isSelectedRow = behavior.IndexOf(row) == -1? false : true;
bool isSelectedItem = listBoxControl1.Items.Contains(value);
if (isSelectedRow && !isSelectedItem)
listBoxControl1.Items.Add(value);
else if (!isSelectedRow && isSelectedItem)
listBoxControl1.Items.Remove(value);
}
}
Important
The behavior handles the view’s SelectionChanged event to update the selection order. To get the updated selection order, handle the behavior’s SelectionChanged event instead of the view’s SelectionChanged event.
Limitations
Ordered Selection Behavior only tracks data rows.
The
CellSelectmulti-selection mode is not supported. Set the GridOptionsSelection.MultiSelectMode property toRowSelectorCheckBoxRowSelect.Data-bound selection is not supported. If you use an
OrderedSelectionBehavior, do not specify the view’s OptionsSelection.CheckBoxSelectorField property.
Notes
If you attach the behavior to a view that already has selected rows, the behavior cannot determine the order in which these rows were selected. To track the actual selection order, attach the behavior before users can select rows (for example, in the form constructor).
Some data source operations reset the selection order. For example, this happens if you remove and re-insert a data row. Run such operations in a
BeginDataUpdate/EndDataUpdatecode block.The behavior may cause performance issues when 1000 or more rows are selected/unselected simultaneously. To avoid performance issues, use the behavior with smaller data sources.