Results 1 to 3 of 3

Thread: [RESOLVED] ComboBox.ValueMember and .SelectedValue without DataSet?

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width