Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Ordered Selection Behavior

    • 3 minutes to read

    Ordered Selection Behavior allows you to track row selection order when multiple selection is enabled.

    WinForms - Ordered Selection Behavior, DevExpress

    You can attach this behavior to Data Grid views.

    Activate Ordered Selection

    1. Activate multiple row selection. Set the view’s OptionsSelection.MultiSelect to true and OptionsSelection.MultiSelectMode to GridMultiSelectMode.RowSelect (or CheckBoxRowSelect).

      Tip

      For more information on multiple row selection, refer to Multiple Row and Cell Selection.

    2. Drop a BehaviorManager onto the form.

    3. Attach an OrderedSelectionBehavior at design time (click Edit Behaviors in the BehaviorManager smart 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

    OrderedSelectionBehavior.GetSelectedRows()

    Returns an array of data row objects in the order rows were selected.

    OrderedSelectionBehavior.IndexOf(object row)

    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

    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/EndDataUpdate code 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.