DevExpress ComboBoxEdit数据源

时间:2012-09-21 09:15:09

标签: c# winforms devexpress

我正在使用DevExpress ComboBoxEdit,我需要将list绑定到其数据源。但是我可以看到没有方法可以添加数据源来控制,所以我添加了每个项目来逐个控制,如

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

它起作用但如果有大量数据则很慢 有没有办法可以直接将列表绑定到控件?

3 个答案:

答案 0 :(得分:8)

无法将ComboBoxEdit直接绑定到数据源,因为ComboBoxEdit旨在在需要一组简单的预定义值时使用。需要使用数据源时使用LookUpEdit 您可以使用ComboBoxItemCollection.BeginUpdateComboBoxItemCollection.EndUpdate方法在更改项目集合时防止过度更新:

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

答案 1 :(得分:1)

这是另一种使用linq one-liner将组合块添加到组合框中的方法:

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

.AddRange()方法在内部调用BeginUpdate()/ EndUpdate()。

答案 2 :(得分:0)

另一种方法是通过扩展方法:

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