사전을 데이터 소스로 사용하여 콤보 상자 바인딩
.NET 2.0을 사용하고 있으며 콤보 상자의 데이터 소스를 정렬 된 사전에 바인딩하려고합니다.
그래서 내가 얻는 오류는 "DataMember 속성 '키'를 데이터 소스에서 찾을 수 없습니다"입니다.
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
SortedDictionary<string, int> userCache = new SortedDictionary<string, int>
{
{"a", 1},
{"b", 2},
{"c", 3}
};
comboBox1.DataSource = new BindingSource(userCache, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
그러나 왜 ValueMember
"Value"로 설정하고 "Key"(및 DisplayMember
"Value" 에도 바인딩)하지 않아야 합니까?
Sorin Comanescu의 솔루션을 사용했지만 선택한 값을 얻으려고 할 때 문제가 발생했습니다. 내 콤보 박스는 툴 스트립 콤보 박스였습니다. 나는 일반 콤보 박스를 노출하는 "combobox"속성을 사용했습니다.
나는
Dictionary<Control, string> controls = new Dictionary<Control, string>();
바인딩 코드 (Sorin Comanescu의 솔루션-매력처럼 작동 함) :
controls.Add(pictureBox1, "Image");
controls.Add(dgvText, "Text");
cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null);
cbFocusedControl.ComboBox.ValueMember = "Key";
cbFocusedControl.ComboBox.DisplayMember = "Value";
문제는 내가 선택한 값을 얻으려고 할 때 그것을 검색하는 방법을 몰랐다는 것입니다. 몇 번의 시도 끝에 나는 이것을 얻었다 :
var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key
다른 사람에게 도움이되기를 바랍니다!
var colors = new Dictionary < string, string > ();
colors["10"] = "Red";
Combobox에 바인딩
comboBox1.DataSource = new BindingSource(colors, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
전체 소스 ... 콤보 박스 데이터 소스로서의 사전
Jeryy
userListComboBox.DataSource = userCache.ToList();
userListComboBox.DisplayMember = "Key";
사전은 데이터 소스로 직접 사용할 수 없으므로 더 많은 작업을 수행해야합니다.
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
KeyValuePair<string, int> [] ar= new KeyValuePair<string,int>[userCache.Count];
userCache.CopyTo(ar, 0);
comboBox1.DataSource = ar; new BindingSource(ar, "Key"); //This line is causing the error
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
이것이 작동하지 않으면 콤보 상자에 모든 항목을 추가하는 사전에 foreach 루프를 수행하지 않는 이유는 무엇입니까?
foreach(var item in userCache)
{
userListComboBox.Items.Add(new ListItem(item.Key, item.Value));
}
사용->
comboBox1.DataSource = colors.ToList();
사전이 목록으로 변환되지 않는 한 콤보 상자는 해당 구성원을 인식 할 수 없습니다.
그냥 이렇게 해봐 ....
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
// Add this code
if(userCache != null)
{
userListComboBox.DataSource = new BindingSource(userCache, null); // Key => null
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
}
참고 URL : https://stackoverflow.com/questions/6412739/binding-combobox-using-dictionary-as-the-datasource
'UFO ET IT' 카테고리의 다른 글
Postgresql 서버의 postgres라는 기본 데이터베이스 (0) | 2020.12.10 |
---|---|
MimeType을 알 수없는 파일에 대한 ACTION_VIEW 인 텐트 (0) | 2020.12.10 |
3.2 sdk 플랫폼 설치 문제. (0) | 2020.12.10 |
Java에서 멤버 변수 재정의 (변수 숨기기) (0) | 2020.12.10 |
SVN은 LC_CTYPE 로케일을 설정할 수 없습니다. (0) | 2020.12.10 |