user1688313
user1688313

Reputation: 51

DevExpress ComboBoxEdit datasource

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?

Upvotes: 3

Views: 38634

Answers (3)

ManxJason
ManxJason

Reputation: 940

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;
    }

Upvotes: 0

XDS
XDS

Reputation: 4188

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.

Upvotes: 4

DmitryG
DmitryG

Reputation: 17850

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();
}

Upvotes: 9

Related Questions