Combobox出現System.Data.DataRowView的原因,以及指定ValueMember的時機問題
阿新 • • 發佈:2019-02-14
- namespace FrmForTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.InitialCombobox();
- }
- private void InitialCombobox()
- {
- DataTable table = new DataTable();
- DataColumn column;
- DataRow row;
- column = new DataColumn("Name");
- table.Columns.Add(column);
- column = new DataColumn("Value");
- table.Columns.Add(column);
- for (int i = 0; i < 5; i++)
- {
- row = table.NewRow();
- row["Name"] = "Test" + i;
- row["Value"] = i.ToString();
- table.Rows.Add(row);
- }
- this.comboBox1.DataSource = table;
- this.comboBox1.DisplayMember = "Name";
- this.comboBox1.ValueMember = "Value";
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.textBox1.Text = this.comboBox1.SelectedValue.ToString();
- }
- }
- }