[RESOLVED] ComboBox.ValueMember and .SelectedValue without DataSet?
I'm just discovering these days that Objects can be added to ComboBoxes, then now i'm adding my Entitiy Objects to each corresponding Combobox whenever i need to fill a ComboBox. I've also learned that using .DisplayMember ComboBox Property i can tell the Combobox what's the property it should show to the user, I like this specially because by doing this i don't need to override my Entity Class ToString() Function. All this is done without using Datasets, i load my controls directly from Lists (Of each corresponding Entity) and this List is filled in the DAL layer.
I've just mentioned Datasets, because now i want to use ValueMemberand SelectedValue to be able to reference a Combobox item by ID, i mean, in the same way that DisplayMember works, allowing me to set the display property in the combobox, i thought i would be able to use SelectedValue to set the selected item by setting its ID, but it looks like ValueMember only works when a Dataset is used to fill the ComboBox, if this is the case, why DisplayMember works without Dataset and ValueMember doesn't? Or maybe i'm mistaken and there is a way to make it work without dataset?
Well, that's the question, now i'm looping to get the correct ComboBox item (entity object) by using the entity ID, is there any other way to do this without looping?
Re: ComboBox.ValueMember and .SelectedValue without DataSet?
I'm not quite sure why they chose to do it this way but ValueMember can only be used when the ComboBox is bound. DataSets are irrelevant though. ComboBoxes, and indeed other WinForms controls, can be bound to any IList or IListSource. You say that you're populating a generic List with your data, so simply bind that List. There's no point looping through the List to get each item and add it manually to the ComboBox. Just bind the List to the ComboBox, first setting the DisplayMember and ValueMember. You can bind arrays too, if that's more convenient.
Re: ComboBox.ValueMember and .SelectedValue without DataSet?
Ahh that's perfect, i didn't know i can use these lists as the Combobox Datasource, then i don't need to loop to fill Comboboxes, and also i can use ValueMember and SelectedValue. Thank you very much