C# - Cannot do Key-Value in a ListBox in WinForms

后端 未结 1 1706
轮回少年
轮回少年 2021-01-12 02:20

I\'m writing a C# app using a ListBox in WinForms.

I get my data (id and full name) from an XML file. I want to show full names in the listbox and when I select on

1条回答
  •  醉话见心
    2021-01-12 02:57

    use c# dictionary for this,

    Dictionary list = new Dictionary();
    list.Add("item 1", "Item 1");
    list.Add("item 2", "Item 2");
    list.Add("item 3", "Item 3");
    list.Add("item 4", "Item 4");
    
    dropdown.DataSource = list;
    dropdown.DataTextField = "Value";
    dropdown.DataValueField = "Key";
    dropdown.DataBind();
    

    EDIT:

    listBox.DataSource = new BindingSource(list, null); 
    listBox.ValueMember = "Key";
    listBox.DisplayMember = "Value"; 
    

    0 讨论(0)
提交回复
热议问题