C# 中combobox 控件初始化

怎么获取绑定数据的combobox的选中值及选中text

cs 复制代码
1、绑定combobox的代码如下:
DataTable LDT_CodeType = Models.MoCodefile.GetCodeType();
if (LDT_CodeType != null)
{
cboxCtype.DataSource = LDT_CodeType;
cboxCtype.DisplayMember = codetypename;
cboxCtype.ValueMember = codetypeno;
}
获取选中值:cboxCtype.SelectedIndex.ToString() 或是 cboxCtype.SelectedValue.ToString()
获取选中text:
方法一:DataRowView dr = (DataRowView)cboxCtype.Items[cboxCtype.SelectedIndex];
方法二:cboxCtype.GetItemText(cboxCtype.Items[cboxCtype.SelectedIndex])
cs 复制代码
        private void Form1_Load(object sender, EventArgs e)
        {

            ArrayList lst = new ArrayList();       //列表
            lst.Add(new Vendor("a_geshi03", "20"));  //在列表中增加对象
            lst.Add(new Vendor("a_geshi04", "30"));

            comboBox1.Items.Clear();                 //清空combobox
            comboBox1.DataSource = lst;           //将lst列表绑定到combobx
            comboBox1.DisplayMember = "Strtemname";// 指定显示的数据项
            comboBox1.ValueMember = "Strindex";  //指定comboBox1.SelectedValue返回的数据项


        }

其中的Vendor可以是自定义的类,如:

cs 复制代码
  class Vendor
    {
        private string strtemname;
        private string strindex;
        public Vendor(string itemname,string index)
        {
            this.strtemname = itemname;
            this.strindex = index;
        }

        public string Strtemname
        {
            get { return strtemname; }
            set { strtemname = value; }
        }

        public string Strindex
        {
            get { return strindex; }
            set { strindex = value; }
        }
    }

二、数据绑定方法

1、绑定List集合:

cs 复制代码
List<string>list = new List<string>()
   {
      "test1","test2","test3"
   };
   this.comboBox1.DataSource = list;

2、绑定数组集合

cs 复制代码
string[] array = new string[] { "test1","test2","test3" };
   this.comboBox1.DataSource = array;

3、绑定数组集合:

cs 复制代码
//创建一个实体类,用于存储数据
public class Data
{
   public string ID { get; set; }
   public string ShowValue { get; set; }
}
 
List<Data> datas = new List<Data>();
private void ArrayDataBinding()
{
            
   Data data1 = new Data() { ID = "1", ShowValue = "test1" };
   Data data2 = new Data() { ID = "2", ShowValue = "test2" };
   Data data3 = new Data() { ID = "3", ShowValue = "test3" };
   datas.Add(data1);
   datas.Add(data2);
   datas.Add(data3);
 
   this.comboBox1.DataSource = datas;
   this.comboBox1.DisplayMember = "ShowValue";
   this.comboBox1.ValueMember = "ID";
}
 
//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   this.textBox1.Text = comboBox1.SelectedValue.ToString();
}
 

4、绑定DataTable

cs 复制代码
private void DataTableDataBinding()
{
    DataTable dt = new DataTable();
    DataColumn dc1 = new DataColumn("ID");
    DataColumn dc2 = new DataColumn("ShowValue");
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
 
    DataRow dr1 = dt.NewRow();
    dr1["ID"] = "1";
    dr1["ShowValue"] = "test1";
    DataRow dr2 = dt.NewRow();
    dr2["ID"] = "2";
    dr2["ShowValue"] = "test2";
    DataRow dr3 = dt.NewRow();
    dr3["ID"] = "3";
    dr3["ShowValue"] = "test3";
 
    dt.Rows.Add(dr1);
    dt.Rows.Add(dr2);
    dt.Rows.Add(dr3);
 
    this.comboBox1.DataSource = dt;
    this.comboBox1.ValueMember = "ID";
    this.comboBox1.DisplayMember = "ShowValue";
}
 
//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.textBox1.Text = comboBox1.SelectedValue.ToString();
}

数据绑定:

cs 复制代码
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "dtColName";
this.comboBox1.ValueMember = "ID";
/*DisplayMember绑定的是需显示的字段,ValueMember绑定的是对应的值
一般DisplayMember是显示给客户看的, 而ValueMember 是绑定处理程序标识 给程序员看的*/

5、绑定枚举

cs 复制代码
//定义一个颜色枚举
public enum ColorEnum
{
   red,
   blue,
   skyblue
}
 
//使用Enum.GetNames()方法,将枚举元素的名称赋值给控件。
private void EnumDataBinding()
{
   this.comboBox1.DataSource = Enum.GetNames(typeof(ColorEnum));
}
相关推荐
Lovyk19 分钟前
完整实验命令解析:从集群搭建到负载均衡配置
linux·运维·服务器
qq_3411604424 分钟前
文件系统挂载详细分析(《图解Linux内核》虚拟文件系统篇笔记二)
linux·服务器·笔记
changuncle31 分钟前
Angular初学者入门第三课——工厂函数(精品)
前端·javascript·angular.js
日月晨曦1 小时前
JavaScript事件循环:一次浏览器线程的"约会"指南
javascript
我不是程序猿儿1 小时前
【C#】观察者模式 + UI 线程调度、委托讲解
观察者模式·ui·c#
s3xysteak1 小时前
我要成为vue高手01:上下文
前端·javascript·vue.js
南篱1 小时前
JavaScript原型链没那么难:一文彻底搞懂
javascript·面试
程序员张31 小时前
Vue3+ElementPlus倒计时示例
javascript·vue.js·前端框架
专注VB编程开发20年1 小时前
c# .net支持 NativeAOT 或 Trimming 的库是什么原理
前端·javascript·c#·.net
熬夜苦读学习1 小时前
Reactor 反应堆模式
运维·服务器·网络·网络协议·http·智能路由器·php