Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Binding 2 ComboBoxes within the same form to a single data source

Binding 2 ComboBoxes within the same form to a single data source

Scheduled Pinned Locked Moved Visual Basic
databasewpfwcftutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    Hi, I'm sure I have seen this question posted before on CP but I am unable to find it if so. I have 2 ComboBoxes on a form both bound to the same data source. But when I change the selected index in the first the second changes also, and vise-versa. Can somebody please give me some hints oh how to do this properly. Thanks. (Just in case it matters, the data source is a generic list of user defined objects)

    D 1 Reply Last reply
    0
    • J Jay Royall

      Hi, I'm sure I have seen this question posted before on CP but I am unable to find it if so. I have 2 ComboBoxes on a form both bound to the same data source. But when I change the selected index in the first the second changes also, and vise-versa. Can somebody please give me some hints oh how to do this properly. Thanks. (Just in case it matters, the data source is a generic list of user defined objects)

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      <EDIT> Sorry - I just reread that and it didn't sound right. It should be BindingSource, not Navigator. Attribute it to rectal-cranial inversion. </EDIT> You have to stick 2 BindingSources's in between the combos and the datasource. Each combo will have it's own BindingSource between it and the datasource. Both BindingSources will have their DataSource properties pointing at the single data source.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      modified on Thursday, July 30, 2009 12:19 PM

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        <EDIT> Sorry - I just reread that and it didn't sound right. It should be BindingSource, not Navigator. Attribute it to rectal-cranial inversion. </EDIT> You have to stick 2 BindingSources's in between the combos and the datasource. Each combo will have it's own BindingSource between it and the datasource. Both BindingSources will have their DataSource properties pointing at the single data source.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        modified on Thursday, July 30, 2009 12:19 PM

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        OK Dave thanks for that :) EDIT: Just tried it and it worked perfectly. Should I be using the BindingSource object to do all of my data binding rather than binding directly to the data source?

        modified on Friday, July 31, 2009 4:19 AM

        D 1 Reply Last reply
        0
        • J Jay Royall

          OK Dave thanks for that :) EDIT: Just tried it and it worked perfectly. Should I be using the BindingSource object to do all of my data binding rather than binding directly to the data source?

          modified on Friday, July 31, 2009 4:19 AM

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Not really. I only use it when I've got multiple controls that need to see and navigate a single datasource independant of other controls. Think of it this way. Every control on a form using the same datasource also uses the same binding manager. The binding manager maintain a currency manager (which has nothing to do with money!) which keeps track of which record is the current record all of the controls are looking at. When one control changes the current record, all the controls get pointed at the new current record. The BindingSource class lets you escape that model and sets up another, seperate set of binding objects that does the same thing, independant of the first, or default set. In your case, technically, you don't need two BindingSource objects. You only need one. I just used two objects for better code readability. What's easier to understand??

          Dim bs As New BindingSource
          
          bs.DataSource = data
          
          ComboBox1.DataSource = data
          ComboBox2.DataSource = bs
          

          or

          Dim bs1 As New BindingSource
          Dim bs2 as New BindingSource
          
          bs1.DataSource = data
          bs2.DataSource = data
          
          ComboBox1.DataSource = bs1
          ComboBox2.DataSource = bs2
          

          They work exactly the same...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          J 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Not really. I only use it when I've got multiple controls that need to see and navigate a single datasource independant of other controls. Think of it this way. Every control on a form using the same datasource also uses the same binding manager. The binding manager maintain a currency manager (which has nothing to do with money!) which keeps track of which record is the current record all of the controls are looking at. When one control changes the current record, all the controls get pointed at the new current record. The BindingSource class lets you escape that model and sets up another, seperate set of binding objects that does the same thing, independant of the first, or default set. In your case, technically, you don't need two BindingSource objects. You only need one. I just used two objects for better code readability. What's easier to understand??

            Dim bs As New BindingSource
            
            bs.DataSource = data
            
            ComboBox1.DataSource = data
            ComboBox2.DataSource = bs
            

            or

            Dim bs1 As New BindingSource
            Dim bs2 as New BindingSource
            
            bs1.DataSource = data
            bs2.DataSource = data
            
            ComboBox1.DataSource = bs1
            ComboBox2.DataSource = bs2
            

            They work exactly the same...

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            J Offline
            J Offline
            Jay Royall
            wrote on last edited by
            #5

            Ah I see, understood :) Thanks for taking the time to explain it so well.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups