Custom Object Bound To Combobox

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Hello,

I have a custom object called AddressTypes. It is simple as it just stores
a Dictionary<int, String> of address types - int value and string description
value.

I would like to bind this to a combobox. I tried binding it directly to the
dictionary which kinda works but I get a display of items that say [1,
Physical], [2, Mailing]....

And what I am looking for is just for it to say: Physical, Mailing... and
then when one of those is selected, its int value is retrievable.

Basically I don't want the int value displayed in the box with the
description.

How do I implement this into my custom class/object so that I can just bind
it have it work correctly?

I do not want to have to loop to populate values but would rather bind
directly.

I don't know what to search for online. Any ideas?

Thanks for reading this!

Rob
 
Rob,

How about inheriting from BindingList?

public class AddressTypes : BindingList

BindingList<AddressTypes> addressTypes = getAddressTypes();

comboBox1.DataSource = addressTypes;
comboBox1.ValueMember = "value";
comboBox1.DisplayMember = "description";

See: http://blogs.msdn.com/dchandnani/archive/2005/03/12/394438.aspx

Kofi Sarfo

Wonderful! Thank you so much Kofi! It works beautifully now. This is what
I did based on your suggestion and link to the article:

--In Form1.cs constructor:
addressTypesBindingSource.DataSource =
AddressTypes.Instance.ReturnBindingListOfTypes();
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

--In the Addresses class:

public BindingList<KeyValuePair<int, String>> ReturnBindingListOfTypes()
{
BindingList<KeyValuePair<int, String>> exp = new
BindingList<KeyValuePair<int, String>>();

foreach (KeyValuePair<int, String> kvp in ATResults)
{
exp.Add(kvp);
}

return exp;
} // end ReturnBindingListOfStates ()

And all is well in the world!

Thanks again!

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top