Results 1 to 5 of 5

Thread: [RESOLVED] Populating the information into a ComboBox... ???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Resolved [RESOLVED] Populating the information into a ComboBox... ???

    I need to populate some information into several ComboBoxes.

    The infromation should be read from the table in my database. Is it possible to read the information and populate it onto a combobox without using data binding?

    I tried to populate several ComboBoxes, but if I use databinding, the ComboBoxes all show the same value. Also, if I select a certain value, it also selects that row in the table, which I do not wish to do...

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

    Re: Populating the information into a ComboBox... ???

    Yes it is possible to do without data-binding but you shouldn't. You should use data-binding but just do it correctly. If you want to bind two ComboBoxes to two different columns in the same DataTable and have them select independently then you do it like this:
    vb.net Code:
    1. Dim view1 As New DataView(myDataTable)
    2. Dim view2 As New DataView(myDataTable)
    3.  
    4. With ComboBox1
    5.     .DisplayMember = "Column1"
    6.     .ValueMember = "ID"
    7.     .DataSource = view1
    8. End With
    9.  
    10. With ComboBox2
    11.     .DisplayMember = "Column2"
    12.     .ValueMember = "ID"
    13.     .DataSource = view2
    14. End With
    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
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Re: Populating the information into a ComboBox... ???

    Quote Originally Posted by jmcilhinney View Post
    Yes it is possible to do without data-binding but you shouldn't. You should use data-binding but just do it correctly. If you want to bind two ComboBoxes to two different columns in the same DataTable and have them select independently then you do it like this:
    vb.net Code:
    1. Dim view1 As New DataView(myDataTable)
    2. Dim view2 As New DataView(myDataTable)
    3.  
    4. With ComboBox1
    5.     .DisplayMember = "Column1"
    6.     .ValueMember = "ID"
    7.     .DataSource = view1
    8. End With
    9.  
    10. With ComboBox2
    11.     .DisplayMember = "Column2"
    12.     .ValueMember = "ID"
    13.     .DataSource = view2
    14. End With

    Thank you!

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Re: Populating the information into a ComboBox... ???

    Quote Originally Posted by jmcilhinney View Post
    Yes it is possible to do without data-binding but you shouldn't. You should use data-binding but just do it correctly. If you want to bind two ComboBoxes to two different columns in the same DataTable and have them select independently then you do it like this:
    Is it possible to add and remove values from the ComboBoxes at this point? I would like the ComboBoxes not to repeat already used values.

    I didn't get it to work with the code below:
    vb.net Code:
    1. Dim view1 As New DataView(myDataTable)
    2. Dim view2 As New DataView(myDataTable)
    3.  
    4. With ComboBox1
    5.     .DisplayMember = "Column1"
    6.     .ValueMember = "ID"
    7.     .DataSource = view1
    8.     .Items.Remove(ComboBox2.Text)
    9.     .Items.Remove(ComboBox3.Text)
    10.     .Items.Remove(ComboBox4.Text)
    11.     .Items.Remove(ComboBox5.Text)
    12. End With
    13.  
    14. With ComboBox2
    15.     .DisplayMember = "Column2"
    16.     .ValueMember = "ID"
    17.     .DataSource = view2
    18.     .Items.Remove(ComboBox1.Text)
    19.     .Items.Remove(ComboBox3.Text)
    20.     .Items.Remove(ComboBox4.Text)
    21.     .Items.Remove(ComboBox5.Text)
    22. End With

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

    Re: Populating the information into a ComboBox... ???

    It's possible but not like that. I would suggest binding each DataView via a BindingSource. You can then handle the CurrentChanged event of each BindingSource and set the Filter property of each other BindingSource to filter out the records that have been selected.
    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

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