ValueMember from ComboBox.Items[i] using WinForms c#

半腔热情 提交于 2019-12-25 09:34:25

问题


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()

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);
}

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))

Any Advice?


回答1:


Gets the key in the key/value pair.

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

Gets the value in the key/value pair.

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


来源:https://stackoverflow.com/questions/27628022/valuemember-from-combobox-itemsi-using-winforms-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!