Search code examples
c#genericscombobox

Bind List to comboBox in C#


I have three comboBoxes in a WinForm. To load those combos with data I created three methods. Two of these methods looks like this:

private void cmbLoadSubjects(List<Subject> subjects)
    {
        BindingSource source = new BindingSource();
        source.DataSource = subjects;

        cmbSubjects.DataSource = source;
        cmbSubjects.DisplayMember = "name";
        cmbSubjects.ValueMember = "id";
    }

private void cmbLoadTeachers(List<Teacher> teachers)
    {
        BindingSource source = new BindingSource();
        source.DataSource = teachers;

        cmbTeachers.DataSource = source;
        cmbTeachers.DisplayMember = "name";
        cmbTeachers.ValueMember = "id";
    }

The idea is to display the subject's name and the teacher's name in the combos.

Combo box showing teachers

Given the fact that these methods are very similar, I tried to create a generic method that can load all the combos. So I came up with this:

private void cargarCombo<T>(ComboBox combo, List<T> data, string displayMember, string valueMember)
    {
        BindingSource source = new BindingSource();
        source.DataSource = data;

        combo.DataSource = source.DataSource;
        cmbEstadoAsistencia.DisplayMember = displayMember;
        cmbEstadoAsistencia.ValueMember = valueMember;
    }

If I use this method to load my combos, it only works fine in one of the combos. In the other combos it shows the typical"WorkspaceName.ClassName". Combo box of teachers when using generic method

I tried loading teachers and then tried loading subjects in the only combo that works fine and it showed what it was supposed to show:

Teacher loaded with generic method Subjects loaded with generic method

But when I try to load the other combos using this generic method, despite the class of the objects, I get "Workspace.Teacher", "Workspace.Subject".

I compared the values of the properties of the three combos trying to find something different in order to discover what could be causing this behavior and they only differ in their name and position.

I tried creating a new combo and loading lists of different class of objects but the problems is still there.

One curious thing is that it all works fine if I use the methods I showed you at the beggining of the question. I mean, I could just make use of those methods and forget about this stupid problem, but I'm curious.. I just don't get why the generic method only works fine in one combo.


Solution

  • I found the problem. The problem is that I'm an idiot sometimes:

    private void cargarCombo<T>(ComboBox combo, List<T> data, string displayMember, string valueMember)
    {
        BindingSource source = new BindingSource();
        source.DataSource = data;
    
        combo.DataSource = source.DataSource;
        cmbEstadoAsistencia.DisplayMember = displayMember;
        cmbEstadoAsistencia.ValueMember = valueMember;
    }
    

    cmbEstadoAsistencia should be change to combo

    I was hardcoding the name of one of the comboBoxes. I'm sorry for posting garbage :(