comboBox控件的DisplayMember和ValueMember
(2018-06-24 13:53:57)
资料来源:网络
C#语言中,combobox控件的DisplayMember和ValueMember这个两个属性,在有些时候非常有用,即保存一个id和这个id对应的内容。
combobox控件的这两个属性只有在绑定数据源时才可用,请看下面的演示代码。
-
private void TestComboBox_Load(object sender, EventArgs e)
-
{
-
DataTable dt = new DataTable();
-
DataColumn column;
-
DataRow row;
-
-
column =new DataColumn();
-
column.DataType = System.Type.GetType("System.Int32");
-
column.ColumnName = "id";
-
dt.Columns.Add (column);
-
column =new DataColumn();
-
column.DataType = System.Type.GetType("System.String");
-
column.ColumnName = "c";
-
dt.Columns.Add(column);
-
-
row = dt.NewRow();
-
row["id"] = 1;
-
row["c"] = "中国" ;
-
dt.Rows.Add(row);
-
-
row = dt.NewRow();
-
row["id"] = 2;
-
row["c"] = "日本";
-
dt.Rows.Add(row);
-
-
row = dt.NewRow();
-
row["id"] = 4;
-
row["c"] = "汗国";
-
dt.Rows.Add(row);
-
-
comboBox1.DataSource = dt;
-
comboBox1.DisplayMember = "c";
-
comboBox1.ValueMember = "id";
-
}
-
-
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
MessageBox.Show("ValueMember is : " + comboBox1.SelectedValue);
-
}
喜欢
0
赠金笔
加载中,请稍候......