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));
}
相关推荐
盛满暮色 风止何安2 小时前
IS-IS 中间系统到中间系统
服务器·网络·网络协议·安全·web安全·网络安全·智能路由器
千亦学不会编程3 小时前
C# 枚举 详解
c#
斯普信专业组3 小时前
Apidog MCP服务器,连接API规范和AI编码助手的桥梁
运维·服务器·人工智能
wayuncn3 小时前
深度解析物理机服务器故障修复时间:影响因素与优化策略
运维·服务器·物理机·服务器租用·服务器托管·物理服务器·哈尔滨idc
一人一萧十只猫�3 小时前
Linux SSH 远程连接全攻略:从加密原理到实战配置(含图解)
linux·服务器·网络
ᖰ・◡・ᖳ4 小时前
JavaScript:PC端特效--缓动动画
开发语言·前端·javascript·css·学习·html5
秦jh_4 小时前
【Linux网络】传输层协议UDP
linux·运维·服务器·网络·udp
万物此臻4 小时前
C#编写软件添加菜单栏
开发语言·c#
饼干ovo4 小时前
Linux下软件安装实战
linux·运维·服务器
2302_799525745 小时前
【Linux】第十八章 调优系统性能
linux·运维·服务器