简体   繁体   中英

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.

显示教师的组合框

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". 使用通用方法时的教师组合框

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:

老师加载了泛型方法 用泛型方法加载的主题

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.

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 :(

Your error could be here

combo.DataSource = source.DataSource;

Change that to

combo.DataSource = source;

也许“主题”类需要在其属性中获取和设置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM