SelectedValue 属性用于将 ComboBox 链接到数据源并希望返回与显示内容不同的值。
例如,一种替代方法可能是创建一个 DataTable,将您的数据库读数放入其中,并在那里分配组合框选定的值和文本。例如;
DataTable dataTable = new DataTable();
//column 1 name, which will be display member
dataTable.Columns.Add(new DataColumn("nameOfYourTextField");
//column 2 name, which will be your value member
dataTable.Columns.Add(new DataColumn("nameOfYourValueField");
//assign your datasource (the datatable) to the combobox
comboBox8.DataSource = dataTable;
//and finally assign your value member (the text you want returning)
comboBox8.ValueMember = "nameOfYourValueField";
//and your display member (the text visible in the combobox)
comboBox8.DisplayMember = "nameOfYourTextField";
然后在你的读者中;
while (reader.Read())
{
//create a new row which matches the signature of your datatable
DataRow row = dataTable.NewRow();
//assign data to the rows, given a certain column name
row["nameOfYourValueField"] = reader[1];
row["nameOfYourTextField"] = reader[0];
//and add the row to the datatable
dataTable.Rows.Add(row);
}