Question

    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?

Was it helpful?

Solution

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.

OTHER TIPS

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";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top