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));
}
相关推荐
xkroy10 分钟前
ajax
前端·javascript·ajax
Yvonne爱编码14 分钟前
AJAX入门-URL、参数查询、案例查询
前端·javascript·ajax
Swift社区39 分钟前
如何解决 Vue2 前端项目为何无法访问本地资源(chunk.js 加载一直 pending/转圈)
开发语言·前端·javascript
大飞pkz1 小时前
【设计模式】题目小练2
开发语言·设计模式·c#·题目小练
啟明起鸣1 小时前
【网络编程】从与 TCP 服务器的对比中探讨出 UDP 协议服务器的并发方案(C 语言)
服务器·c语言·开发语言·网络·tcp/ip·udp
清风细雨_林木木1 小时前
Vue加载资源‘如图片’的“直接引入“方式和“request()“的区别
前端·javascript·vue.js
九章云极AladdinEdu1 小时前
深度学习优化器进化史:从SGD到AdamW的原理与选择
linux·服务器·开发语言·网络·人工智能·深度学习·gpu算力
BillKu2 小时前
Vue3应用执行流程详解
前端·javascript·vue.js
欧阳天风2 小时前
链表运用到响应式中
javascript·数据结构·链表
zzzsde2 小时前
【Linux】初识Linux
linux·运维·服务器