12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-12-04 12:25:08
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在C#中使用ComboBox控件显示值和参数有几种常用的方法:使用数据绑定:可以通过设置ComboBox的DataSource属性来绑定数据源,然后设置DisplayMember和ValueMember属性来指定显示值
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在C#中使用ComboBox控件显示值和参数有几种常用的方法:
// 假设有一个包含值和参数的数据源List<KeyValuePair<string, int>> data = new List<KeyValuePair<string, int>>{new KeyValuePair<string, int>("Value1", 1),new KeyValuePair<string, int>("Value2", 2),new KeyValuePair<string, int>("Value3", 3)};// 绑定数据源comboBox.DataSource = data;// 设置显示值和参数的字段名comboBox.DisplayMember = "Key";comboBox.ValueMember = "Value";
comboBox.Items.Add(new KeyValuePair<string, int>("Value1", 1));comboBox.Items.Add(new KeyValuePair<string, int>("Value2", 2));comboBox.Items.Add(new KeyValuePair<string, int>("Value3", 3));// 获取选中项的参数值int param = ((KeyValuePair<string, int>)comboBox.SelectedItem).Value;
public class CustomItem{public string DisplayValue { get; set; }public int ParamValue { get; set; }public override string ToString(){return DisplayValue;}}// 创建自定义类的集合List<CustomItem> items = new List<CustomItem>{new CustomItem { DisplayValue = "Value1", ParamValue = 1 },new CustomItem { DisplayValue = "Value2", ParamValue = 2 },new CustomItem { DisplayValue = "Value3", ParamValue = 3 }};// 绑定数据源comboBox.DataSource = items;comboBox.DisplayMember = "DisplayValue";comboBox.ValueMember = "ParamValue";
以上是几种常用的方法来在C#中使用ComboBox控件显示值和参数,可以根据具体需求选择合适的方式来实现。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19