1

我以这种方式使用基础值和显示值填充我的组合框:

Dictionary<int, string> Platypi = duckBillData.GetPlatypiVals();
comboBoxPlatypus.DataSource = new BindingSource(Platypi, null);
comboBoxPlatypus.ValueMember = "Key";
comboBoxPlatypus.DisplayMember = "Value";

现在我想提取所选项目的 ValueMember。我怎么做?“显而易见”的事情似乎都没有“ValueMember”......我试过了:

int id = comboBoxPlatypus.ValueMember;
int id = comboBoxPlatypus.SelectedIndex. <-- no "ValueMember" here...
int id = comboBoxPlatypus.SelectedItem. <-- no "ValueMember" here...
4

1 回答 1

2

ValueMember告诉组合框要绑定到原始集合的哪个属性,它不是绑定对象本身。 SelectedItem应该包含您的结果值。您只需要将其转换为正确的类型,在这种情况下,这就是您的int密钥。

int id = (int) comboBox.SelectedItem;
于 2012-09-17T23:24:50.573 回答