comboBox控件的DisplayMember和ValueMember

combobox的这个功能在有些时候非常有用,即保存一个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); }

你可能感兴趣的:(Windows,Phone)