3
votes

I am using DevExpress ComboBoxEdit and I need to bind list to its datasource. But as I can see there is no method to add datasource to control, so I added each item to control one by one like

foreach (var item in list) {
    comboBoxEdit1.Properties.Items.Add(item);
}

It worked for but it is slow if there is lot of data.
Is there a way where I can bind list directly to control?

3

3 Answers

9
votes

There is no way to bind the ComboBoxEdit directly to the datasource because the ComboBoxEdit is designed to be used when you need a simple predefined set of values. Use the LookUpEdit when you need to use a datasource.
You can use the the ComboBoxItemCollection.BeginUpdate and ComboBoxItemCollection.EndUpdate methods to prevent excessive updates while changing the item collection:

ComboBoxItemCollection itemsCollection = comboBoxEdit1.Properties.Items;
itemsCollection.BeginUpdate();
try {
    foreach (var item in list) 
        itemsCollection.Add(item);
}
finally {
    itemsCollection.EndUpdate();
}
4
votes

Here's another approach to add items en-mass to a combobox using a linq one-liner:

  comboBoxEdit1.Properties.Items.AddRange(newItems.Select(x => x.SomeStringPropertyHere as object).ToArray());

The .AddRange() method takes care to invoke BeginUpdate()/EndUpdate() internally.

0
votes

And another approach is through an extension method:

    public static ComboBoxEdit AddItemsToCombo(this ComboBoxEdit combo, IEnumerable<object> items)
    {
        items.ForEach(i => combo.Properties.Items.Add(i));
        return combo;
    }