Cannot bind to the new display member in ComboBox

后端 未结 9 729
迷失自我
迷失自我 2021-01-01 21:14

I have a class which give me this error

public class Item 
{
    public string Name;
    public int Id

    public Item(string name, int id) 
    {
        N         


        
9条回答
  •  粉色の甜心
    2021-01-01 21:46

    My issue was that my dataSource was of Type List < string > so I just test for that

    public static void AddDataSource(this ComboBox comboBox, T dataSource, string item)
    {
        comboBox.DataSource = dataSource;
    
        if (!typeof(T).Equals(typeof(List))) // <-- Here
        {
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "Value";
        }
    
        comboBox.SelectedIndex = -1;
        comboBox.Text = $"Select a {item}";
    }
    

提交回复
热议问题