WinForms Lookup — Multiple Item Selection (v23.1)

WinForms Team Blog
24 July 2023

We recently introduced a highly requested feature for our WinForms Lookup Editor — Multiple Item Selection. In this new selection mode, the Lookup Editor displays a column with checkboxes. Users can select lookup items with the mouse or keyboard.

Enable Multiple Item Selection

Use the EditValueType property to enable multiple item selection. This property specifies how the Lookup stores selected values in its EditValue property. Available options include:

  • LookUpEditValueType.ValueList — the Lookup stores selected values as a list of objects (List<object>).
  • LookUpEditValueType.CSVString — the Lookup stores selected values as a string with comma separated values.

The following example demonstrates how to activate multiple item selection. I set the lookup's EditValue property to a list with IDs to select corresponding products on app startup.

using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

public partial class Form1 : XtraForm {
    public Form1() {
        InitializeComponent();
        lookUpEdit1.Properties.DataSource = ProductA.GetList();
        lookUpEdit1.Properties.DisplayMember = "Name";
        lookUpEdit1.Properties.ValueMember = "ID";
        lookUpEdit1.Properties.EditValueType = LookUpEditValueType.ValueList;
        lookUpEdit1.EditValue = new List<object>() {0, 1, 4};
    }
}
public class ProductA {
    int id;
    public ProductA(int id) {
        this.id = id;
    }
    [Display(Order = -1)]
    public int ID {
        get { return id; }
    }
    [Display(ShortName = "Bakery & Desserts")]
    public string Name { get; set; }
    [DisplayFormat(DataFormatString = "c2")]
    public double Price { get; set; }
    public static List<ProductA> GetList() {
        return new List<ProductA>() {
            new ProductA(0){ Name = "Butter Cake", Price = 56.99 },
            new ProductA(1){ Name = "Chocolate Pie ", Price = 89.99 },
            new ProductA(2){ Name = "Frozen Cookie Dough", Price = 54.99 },
            new ProductA(3){ Name = "Truffie Cake", Price = 59.99 },
            new ProductA(4){ Name = "Original Apple Pie", Price = 59.99 }
        };
    }
}
Multiple Item Selection — WinForms LookUpEdit Control, DevExpress

Bind Selected State to Data

The Lookup Editor can persist information about selected items in a data source field. Use the CheckBoxSelectorName property to specify the field name with information about selected items. If this field contains non-Boolean value types, you should also handle the following events to "convert" field values as needed:

How to Bind to a Boolean Data Field

The following example demonstrates how to bind selection to the "Selected" field (with Boolean values):

public partial class Form1 : XtraForm {
    public Form1() {
        InitializeComponent();
        lookUpEdit2.Properties.DataSource = ProductB.GetList();
        lookUpEdit2.Properties.DisplayMember = "Name";
        lookUpEdit2.Properties.ValueMember = "ID";
        lookUpEdit2.Properties.CheckBoxSelectorMember = "Selected";
        lookUpEdit2.Properties.EditValueType = LookUpEditValueType.ValueList;
        lookUpEdit2.EditValue = new List<object>() {0, 1};
    }
}
public class ProductB {
    int id;
    public ProductB(int id) {
        this.id = id;
    }
    [Display(Order = -1)]
    public int ID {
        get { return id; }
    }
    [Display(ShortName = "Bakery & Desserts")]
    public string Name { get; set; }
    [DisplayFormat(DataFormatString = "c2")]
    public double Price { get; set; }
    public int InStock { get; set; }
    [Display(ShortName = "Add to Order")]
    public bool Selected { get; set; }
    public static List<ProductB> GetProductList() {
        return new List<ProductB>() {
            new ProductB(0){ Name = "Butter Cake", Price = 56.99, InStock = 50 },
            new ProductB(1){ Name = "Chocolate Pie ", Price = 89.99, InStock = 32 },
            new ProductB(2){ Name = "Frozen Cookie Dough", Price = 54.99, InStock = 0 },
            new ProductB(3){ Name = "Truffie Cake", Price = 59.99, InStock = 42 },
            new ProductB(4){ Name = "Original Apple Pie", Price = 59.99, InStock = 0}
        };
    }
}

As you can see from the screenshot below, the lookup creates a selector column (Add to Order) and binds it to the "Selected" data field.

Multiple Item Selection Bound Mode — WinForms LookUpEdit Control, DevExpress

How to Bind to a Collection Property

If the EditValueType property is set to LookUpEditValueType.ValueList, you can bind the lookup's EditValue property to a read-only collection type property. This behavior is controlled by the EnableEditValueCollectionEditing setting (it is enabled by default).

Multiple Item Selection Bind to Collection Property — WinForms LookUpEdit Control, DevExpress
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;

public partial class Form1 : XtraForm {
    List<Item> orderItems;
    List<Order> orders;
    public Form1() {
        InitializeComponent();
        orders = new List<Order>(){
            new Order(10295),
            new Order(10296),
            new Order(10297)
        };
        teOrder.DataBindings.Add(new Binding("EditValue", orders, "ID"));
        dataNavigator1.DataSource = orders;

        orderItems = new List<Item>() {
            new Item(){ ID = 0, ItemName = "Butter Cake", Price = 56.99 },
            new Item(){ ID = 1, ItemName = "Chocolate Pie", Price = 89.99},
            new Item(){ ID = 2, ItemName = "Frozen Cookie Dough", Price = 54.99},
            new Item(){ ID = 3, ItemName = "Truffie Cake", Price = 59.99 },
            new Item(){ ID = 4, ItemName = "Original Apple Pie", Price = 59.99 }
        };
        lookupOrderItems.Properties.DataSource = orderItems;
        lookupOrderItems.Properties.DisplayMember = "ItemName";
        lookupOrderItems.Properties.ValueMember = "ID";
        lookupOrderItems.Properties.EditValueType = LookUpEditValueType.ValueList;
        lookupOrderItems.DataBindings.Add(new Binding("Editvalue", orders, "Items"));
    }
}

public class Order {
    int id;
    public Order(int id) {
        this.id = id;
        Items = new HashSet<int>();
    }
    [Display(ShortName = "Order ID")]
    public int ID { get { return id; } }
    public HashSet<int> Items { get; private set; }
}
public class Item {
    public int ID { get; set; }
    public string ItemName { get; set; }
    public double Price { get; set; }
}

Conditional Selection

With this option, you can prevent users from selecting items based on a specific condition. Handle the SelectionChanging event and set the e.Cancel parameter to true to cancel item selection.

using DevExpress.XtraEditors.Controls;

public partial class Form1 : XtraForm {
	List<ProductB> products;
    public Form1() {
        InitializeComponent();
        //...
        lookUpEdit1.Properties.SelectionChanging += Properties_SelectionChanging;
    }
    private void Properties_SelectionChanging(object sender, PopupSelectionChangingEventArgs e) {
        e.Cancel = products[e.RecordIndex].InStock == 0;
    }
}

Review the following help topic for additional information and API definitions:
Multiple Item Selection in WinForms LookUpEdit.

 

IMPORTANT

Multiple item selection is available for the WinForms LookUpEdit control. GridLookUpEdit, TreeListLookUpEdit, and SearchLookUpEdit controls do not support this capability.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.