[英]How to add function to add null value to combobox
I have many class to set ComboBox datasource 我有很多可以设置ComboBox数据源的类
- Position [DISPLAY, VALUE, ID, NAME]
- Division [DISPLAY, VALUE, ID, NAME]
- SubDivision [DISPLAY, VALUE, ID, NAME, DIVISIONID]
- ect.
and i binding data 我绑定数据
List<Position> list = new List<Position>;
list.Add(...);
cboPosision.DataSource = list;
How to create method for ComboBox to insert row Null data 如何为ComboBox创建方法以插入行Null数据
private void SetDataSource(this ComboBox cbo, object dataList, bool IncludeAll)
{
if(includeAll) { dataList.Add(null); } //Need Insert object {DISPLAY:"All", VALUE:null}
cbo.DataSource = dataList;
}
Here is one way to do it: 这是一种实现方法:
Create a common interface that all your combobox items must implement: 创建一个公共接口,所有组合框项目都必须实现:
interface IComboBoxItem
{
string Display {get;set;}
object Value {get;set;
}
Then use a generic extension method to set that list as a data source: 然后,使用通用扩展方法将该列表设置为数据源:
private void SetDataSource<T>(this ComboBox cbo, IList<T> dataList, bool IncludeAll) where T : new, IComboBoxItem
{
if(includeAll)
{
dataList.Add(new T() {Display = "All", Value = null});
}
cbo.DisplayMember = "Display"; // This corresponds to the display member of the data object
cbo.ValueMember = "Value"; // this corresponds to the value member of the data object
cbo.DataSource = dataList;
}
Note: code written directly here, there might be some typos. 注意:此处直接编写代码,可能会有一些错别字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.