使用WinForms c#从ComboBox.Items [i]中获取ValueMember [英] ValueMember from ComboBox.Items[i] using WinForms c#

查看:375
本文介绍了使用WinForms c#从ComboBox.Items [i]中获取ValueMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码填写组合框:

I fill out a ComboBox using below code:

cbxLines.DisplayMember = "Value";
cbxLines.ValueMember = "Key";
cbxLines.DataSource = new BindingSource(GetProductionLines(), null);

private Dictionary<int, string> GetProductionLines()

现在我想用每个 DisplayMember 以及其他信息:

Now I want to fill out a ListView with every DisplayMember from the ComboBox among other info:

lvSelectedSetup.Items.Clear();
for (int i = 0; i <= cbxLines.Items.Count - 1; i++)
{
     ListViewItem item = new ListViewItem();
     item.SubItems.Add(cbxLines.Items[i].ToString());  <-- How to Get DisplayMember
     item.SubItems.Add(cbxFromDate.Text);
     item.SubItems.Add(cbxToDate.Text);
     lvSelectedSetup.Items.Add(item);
}

但我都不知道如何获得 ComboBox中的ValueMember DisplayMember

But I don't know how to get either the ValueMember or DisplayMember from the ComboBox.

我正在尝试执行以下操作,但会卡住:

I was trying doing the following, but get stuck:

item.SubItems.Add(cbxLines.Items[i].GetType().GetProperty(cbxLines.ValueMember).GetValue(cbxLines,null))

有任何建议吗?

推荐答案

获取键/值对中的键。

Gets the key in the key/value pair.

   ((KeyValuePair<int, string>)cbxLines.Items[i]).Key

获取键/值对中的值。

((KeyValuePair<int, string>)cbxLines.Items[i]).Value

这篇关于使用WinForms c#从ComboBox.Items [i]中获取ValueMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆