سؤال

I needed to give a name to a list, as well as implementing one or two useful methods in addition. So this is my program architecture:

public class A 
{
    public void Method1()
    {
    ...
    }

    public override string ToString()
    {
    ...
    }
}

public class B : List<A>
{
    public override string ToString()
    {
    ...
    }

    public void Method2()
    {
    ...
    }
}

And then, in my mainform, I'm creating and instancing:

BindingList<B> MyList = new BindingList<B> MyList();

To use it with a CheckedListBox, I'm using:

MyListBox.DataSource = MyList;

And then when I'm adding a new element to MyList, I've got the wrong text in the CheckedListBox. Indeed, it shows just one string "(Collection)", even if I have more than one item in MyList What's wrong with my code?

هل كانت مفيدة؟

المحلول

It's not binding the List to your DataSource correctly.

Instead of passing in the list, you could add them like the first answer in Using datasource with CheckBoxList

foreach (var item in MyList)
    MyListBox.Items.Add(item.WhateverFieldYouWantToBind, false); // second param is whether it's checked or not
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top