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));
}
相关推荐
Linux-palpitate14 分钟前
Keepalived+LVS实现LNMP网站的高可用部署
linux·运维·服务器·mysql·lvs
我血条子呢27 分钟前
动态组件和插槽
前端·javascript·vue.js
前端付豪36 分钟前
13、表格系统架构:列配置、嵌套数据、复杂交互
前端·javascript·架构
南屿im42 分钟前
发布订阅模式和观察者模式傻傻分不清?一文搞懂两大设计模式
前端·javascript
JustHappy44 分钟前
SPA?MPA?有啥关系?有啥区别?聊一聊页面形态 or 路由模式
前端·javascript·架构
每天开心44 分钟前
🧙‍♂️闭包应用场景之--防抖和节流
前端·javascript·面试
FileLink跨网文件交换1 小时前
跨网文件交换?内外网文件交换十大方法构建安全合规的数据传输通道
运维·服务器·网络
️️(^~^)1 小时前
静态路由综合配置实验报告
服务器·网络·计算机网络·智能路由器
归于尽1 小时前
Generator?从 yield 卡壳,到终于搞懂协程那点事
前端·javascript
FogLetter1 小时前
React组件开发进阶:本地存储与自定义Hooks的艺术
前端·javascript·react.js