combobox绑定
电脑版发表于:2021/12/9 12:29
前言
实用小功能
combobox绑定一个value
//定义一个datatable 实例化出每一行 DataTable sourcettable = new DataTable(); sourcettable.Columns.Add("Name"); sourcettable.Columns.Add("Code"); DataRow dr1 = sourcettable.NewRow(); dr1["Name"] = "节育相关检查"; dr1["Code"] = "917"; sourcettable.Rows.Add(dr1); comboBox2.DataSource = sourcettable;//设置数据源 comboBox2.DisplayMember = "Name";//设置显示列 comboBox2.ValueMember = "Code";//设置实际值 comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
注意;DataSource 设置过后不能再对combobox2的元素进行更改
现在 comboBox2.SelectedValue.ToString(); 就等于绑定的code