Reputation: 15725
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.)
Upvotes: 2
Views: 344
Reputation: 4311
You can try this:
comboBox1.FormattingEnabled = false;
I have tried it and now the combobox shows the text 'Class' and not "(Collection)".
Upvotes: 4
Reputation: 20157
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.
Upvotes: 1