tags:

views:

38

answers:

2

I have the following code:

public partial class Form1 : Form
{
    public BindingList<Class> List = new BindingList<Class>();

    public Form1()
    {
        InitializeComponent();
        List.Add(new Class());
        comboBox1.DataSource = List;
    }
}

public class Class : Dictionary<string, string>
{
    public override string ToString()
    {
        return "Class";
    }
}

The problem is that the combo box shows the text "(Collection)" instead of "Class". Whenever I remove the Dictionary<string, string> as an ancestor of Class, the code show's "Class" as I want it to. Even when I derive it from another base class I wrote myself it works OK. This is of course not the real program but the problem isolated. So am I missing something or I don't have a choice other than declare a Dictionary<string, string> field in Class instead of subclassing it? (Oh, and by the way, I already tried adding a breakpoint in the ToString function and it's not being called.)

+1  A: 

You could declare your own BindingList subclass which also overrides ToString() and returns the value of ToString() of one of its elements:

public class MyBindingList<T> : BindingList<T>
{
    public override string ToString()
    {
        if (this.Count == 0)
        {
            return "Empty BindingList";
        }

        return "BindingList of " + this[0].ToString();
    }
}

Then just use MyBindingList where you use BindingList in your code.

Jesse C. Slicer
+3  A: 

You can try this:

comboBox1.FormattingEnabled = false;

I have tried it and now the combobox shows the text 'Class' and not "(Collection)".

Martin Ingvar Kofoed Jensen
The .NET databinding implementation is automatically formatting your collection class for you if the Control.FormattingEnabled flag is true. If you want an extended explanation, check out:http://msdn.microsoft.com/en-us/library/aa480734.aspx
Matt
that's perfect. thank you guys
jsoldi