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));
}
相关推荐
工业甲酰苯胺3 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
saynaihe4 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
小蜜蜂爱编程4 小时前
ubuntu透网方案
运维·服务器·ubuntu
时光追逐者4 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 46 期(2025年7.7-7.13)
c#·.net·.netcore
mit6.8245 小时前
Why C# and .NET are still relevant in 2025
c#
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程
头发那是一根不剩了5 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
军训猫猫头6 小时前
5.浏览本地文件获取路径与文件名称 C#例子 WPF例子
开发语言·c#·wpf
谢尔登7 小时前
【React Native】ScrollView 和 FlatList 组件
javascript·react native·react.js
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法