C# DevExpress ComboBoxEdit 数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12527615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DevExpress ComboBoxEdit datasource
提问by user1688313
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
我正在使用 DevExpress ComboBoxEdit,我需要将列表绑定到它的数据源。但是我看到没有办法给控件添加数据源,所以我把每一项都添加到控件中,就像
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?
它有效,但如果有大量数据,它会很慢。
有没有办法可以直接将列表绑定到控件?
回答by DmitryG
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 LookUpEditwhen you need to use a datasource.
You can use the the ComboBoxItemCollection.BeginUpdateand ComboBoxItemCollection.EndUpdatemethods to prevent excessive updates while changing the item collection:
无法将 ComboBoxEdit 直接绑定到数据源,因为 ComboBoxEdit 旨在在您需要一组简单的预定义值时使用。当您需要使用数据源时,请使用LookUpEdit。
您可以使用ComboBoxItemCollection.BeginUpdate和ComboBoxItemCollection.EndUpdate方法来防止在更改项目集合时进行过度更新:
ComboBoxItemCollection itemsCollection = comboBoxEdit1.Properties.Items;
itemsCollection.BeginUpdate();
try {
foreach (var item in list)
itemsCollection.Add(item);
}
finally {
itemsCollection.EndUpdate();
}
回答by XDS
Here's another approach to add items en-mass to a combobox using a linq one-liner:
这是使用 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.
.AddRange() 方法负责在内部调用 BeginUpdate()/EndUpdate()。
回答by ManxJason
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;
}

