I am attempting to store metadata in my ComboBox elements by overriding a Dictionary<>’s ToString(). However, my ToString() override does not appear to be executing, and I can’t figure out why. The ComboBox displays “(Collection)” instead of the value I specify. Is there a step I’m missing to get the ComboBox to use my ToString() override?
public class ComboElement : Dictionary<string,object> {
protected string defaultkey = "";
public ComboElement( SqlDataReader sdr, string defkey )
: base() {
defaultkey = defkey;
for ( int field = 0; field < sdr.FieldCount; field++ ) {
this.Add( sdr.GetName( field ), sdr[field] );
}
}
public override string ToString() {
return "GLURP"; //
//if ( this.ContainsKey( this.defaultkey ) == true ) { return this[this.defaultkey].ToString(); } else return "";
}
}
Code to populate ComboBox on DropDown:
while ( sdr.Read() == true ) {
ComboElement ce = new ComboElement( sdr, "filename" );
string tstring = ce.ToString(); // Correct value
cmbFiles.Items.Add( ce ); // Displays "(Collection)"
}
I would try adding a
DisplayValueproperty to yourComboElementclass and then setting theDisplayMemberof theComboBoxto the name of this property.