2012-09-17 5 views
1

가 나는 기본 값과 표시 값과이 방법을 내 콤보 상자를 채우는 해요 :comboBox에서 포함 된 값을 추출하는 방법은 무엇입니까?

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

지금 내가 선택한 항목의 ValueMember을 추출 할. 어떻게해야합니까? 가 아닌,

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

답변

2

ValueMember바인드에있는 원래의 모음의 속성에 콤보 상자를 알려줍니다은 "명백한"일 중에 내가 해봤 ...는 "ValueMember"이 보이지 않는다 바운드 객체 ​​자체. SelectedItem에는 결과 값이 포함되어야합니다. 올바른 유형 (이 경우에는 int 키)으로 변환하면됩니다.

int id = (int) comboBox.SelectedItem; 
+0

실제로 다음과 같이 사용합니다. int callFlowID = (int) comboBoxCallFlow.SelectedItem; "System.InvalidCastException 처리되지 않았습니다. 메시지 = 지정한 캐스트가 유효하지 않습니다." –

+0

사전에있는 키 (또는 ValueMember로 지정한 항목)가 여전히 int입니까? –

+0

이것은 또한 실패합니다 : int callFlowID = Convert.ToInt32 (comboBoxCallFlow.ValueMember); ... ValueMember의 값이 "Key"... ??? –

관련 문제