views:

566

answers:

2
    public class FontType
    {
        ...
        public String Name { get { return _name; } }
        public String DisplayName { get { return _displayName; } }
        public Font UseFont { get { return _font; } }
    }


bindFontTypes.DataSource = availableFonts;
comboFontType.DataSource = bindFontTypes;
comboFontType.ValueMember = "Key";
comboFontType.DisplayMember = ...???;

Here, bindFontTypes is BindingSource. availableFonts is a Hashtable where Keys are strings, and Values are objects of FontType. For comboFontType.DisplayMember I want to use objects' .DisplayName property. How do I specify that? Is it possible?

+1  A: 

It might work if you set

comboFontType.DisplayMember = "Value";  // FontType

and overload ToString() for FontType.

As an alternative for ToString() you can handle the Format event of the combobox.

But I'm not even sure if the databinding works this way.

Henk Holterman
It works, thank you!
flamey
+1  A: 

By using DisplayMember = "Value.DisplayName" I am geting the last one added to the Hashtable...I am working on getting them all....

This is what I did...but only get the last item in the Hashtable to bind....

BindingSource src = new BindingSource();
            src.DataSource = new Hashtable 
            { 
            {
                "blah", 
                new FontType 
                { 
                    Name = "newFont", 
                    DisplayName = "new Font" 
                } 
                },
                { 
                    "another", 
                    new FontType 
                    {
                        Name = "anotherFont",
                        DisplayName = "another Font" 
                    } 
                    } 
            };
            comboBox1.DataSource = src;
            comboBox1.ValueMember = "Key";
            comboBox1.DisplayMember = "Value.DisplayName";
CSharpAtl
This gives me "Object reference not set to an instance of an object" somewhere. It's not stand alone program, and it's pain to debug. the other solution worked. But thanks for reply.
flamey