2014-12-23 1 views
0

c를 내가 코드 아래 사용하여 콤보를 작성ValueMember는 [내가]를 사용하여 윈폼은 #

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에서 또는 DisplayMember 중 하나를 얻는 방법을 모른다.

나는 다음을 수행하려고했지만, 박히 :

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

어떤 조언을?

답변

5

키/값 쌍의 키를 가져옵니다.

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

키/값 쌍의 값을 가져옵니다.

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

뛰어난! 그건 그렇고, 내 접근 방식을 만드는 방법을 알아 냈어 :'item.SubItems.Add (cbxLines.Items [0] .GetType(). GetProperty (cbxLines.DisplayMember) .GetValue (cbxLines.Items [i] , null) .ToString());'조금 더 길다. :) – Somebody

+0

@ 사람 : 야곱이 보여주는 방식보다 느리다. 가능한 경우 반사를 피하십시오. 그리고 대개 할 수 있습니다. 'DisplayMember' 멤버에 대해서만 이것을 필요로합니다. ComboBox.SelectedValue' 속성에서 바로 ValueMember 멤버의 값을 가져올 수 있습니다. –

관련 문제