Has*_*aan 4 c# combobox keyvaluepair
我想获得的关键SelectedItem的ComboBox,但不弄清楚如何让我做的是代码,
void CboBoxSortingDatagridview(ComboBox sender)
{
foreach (var v in DictionaryCellValueNeeded)
{
if (!DictionaryGeneralUsers.ContainsKey(v.Key) && v.Value.RoleId == Convert.ToInt32(((ComboBox)sender).SelectedItem)) // here getting value {1,Admin} i want key value which is 1 but how?
{
DictionaryGeneralUsers.Add(v.Key, (GeneralUser)v.Value);
}
}
dataGridViewMain.DataSource = DictionaryGeneralUsers.Values;
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式绑定了组合框,
cboRolesList.DataSource = new BindingSource(dictionaryRole, null);
cboRolesList.DisplayMember = "Value";
cboRolesList.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)
Mic*_*ter 13
在这种情况下,字典只是键值对的集合,因此每个项目ComboBox都是a KeyValuePair<YourKeyType, YourValueType>.转换SelectedItem为a KeyValuePair<YourKeyType, YourValueType>然后你可以读取密钥.
// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;
// get selected KVP
KeyValuePair<YourKeyType, YourValueType> selectedEntry
= (KeyValuePair<YourKeyType, YourValueType>) comboBox.SelectedItem;
// get selected Key
YourKeyType selectedKey = selectedEntry.Key;
Run Code Online (Sandbox Code Playgroud)
或者,更简单的方法是使用该SelectedValue属性.
// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;
// get selected Key
YourKeyType selectedKey = (YourKeyType) comboBox.SelectedValue;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15430 次 |
| 最近记录: |