none
Bind same data source to multiple combo boxes easy way? RRS feed

  • Question

  • Hallo , i would like to bind multiple combo boxes in the form to same data source. What is the easiest way to achieve this?.  All combo boxes uses same display member and value member. 

    I want to apply same code to multiple combo boxes like combobox2, combobox3........etc

                With Me.Combobox1
                    .DataSource = New BindingSource(Currency_Dic, Nothing)
                    .DisplayMember = "value"
                    .ValueMember = "key"
                End With
    Thanks

    Saturday, July 20, 2019 1:38 PM

Answers

  • Hi

    Betcha I have misunderstood your question. Anyway here is my interpretation.

    To set upo a series of Comboboxes.

    In this example, all CB are assumed to be on Form1 itseld and notin any oither container (Panel etc) [although those can also be catered for]

    This sets all CB to the same datasource and same Display member and Value member.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable
    
    	Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("one")
    			.Columns.Add("two")
    			.Columns.Add("three")
    
    			For i As Integer = 11 To 29
    				.Rows.Add(i, i * 7, i / 3)
    			Next
    		End With
    		DGV.DataSource = dt
    
    		For Each c As Control In Controls
    			If c.GetType Is GetType(ComboBox) Then
    				Dim cb As ComboBox = DirectCast(c, ComboBox)
    				With cb
    					.DataSource = dt.DefaultView.ToTable
    					.DisplayMember = "one"
    					.ValueMember = "three"
    					AddHandler .SelectedIndexChanged, AddressOf CB_SelectedIndexChanged
    				End With
    			End If
    		Next
    	End Sub
    	Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs)
    		Dim cb As ComboBox = DirectCast(sender, ComboBox)
    		Label1.Text = cb.Name & " : " & cb.SelectedValue.ToString
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Sunday, July 21, 2019 7:47 PM
    Saturday, July 20, 2019 8:22 PM

All replies

  • Hi

    Betcha I have misunderstood your question. Anyway here is my interpretation.

    To set upo a series of Comboboxes.

    In this example, all CB are assumed to be on Form1 itseld and notin any oither container (Panel etc) [although those can also be catered for]

    This sets all CB to the same datasource and same Display member and Value member.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable
    
    	Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("one")
    			.Columns.Add("two")
    			.Columns.Add("three")
    
    			For i As Integer = 11 To 29
    				.Rows.Add(i, i * 7, i / 3)
    			Next
    		End With
    		DGV.DataSource = dt
    
    		For Each c As Control In Controls
    			If c.GetType Is GetType(ComboBox) Then
    				Dim cb As ComboBox = DirectCast(c, ComboBox)
    				With cb
    					.DataSource = dt.DefaultView.ToTable
    					.DisplayMember = "one"
    					.ValueMember = "three"
    					AddHandler .SelectedIndexChanged, AddressOf CB_SelectedIndexChanged
    				End With
    			End If
    		Next
    	End Sub
    	Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs)
    		Dim cb As ComboBox = DirectCast(sender, ComboBox)
    		Label1.Text = cb.Name & " : " & cb.SelectedValue.ToString
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Shan1986 Sunday, July 21, 2019 7:47 PM
    Saturday, July 20, 2019 8:22 PM
  • Hello,

    High level, retrieve the data into one container, make a copy for each ComboBox else when selecting a value in one ComboBox the others will have the same selected value/selected index. So if Currency_Dic is the source then create three BindingSource components and fee the Currency_Dic into each one.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, July 21, 2019 4:42 AM
    Moderator
  • Hi

    Betcha I have misunderstood your question. Anyway here is my interpretation.

    To set upo a series of Comboboxes.

    In this example, all CB are assumed to be on Form1 itseld and notin any oither container (Panel etc) [although those can also be catered for]

    This sets all CB to the same datasource and same Display member and Value member.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim dt As New DataTable
    
    	Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("one")
    			.Columns.Add("two")
    			.Columns.Add("three")
    
    			For i As Integer = 11 To 29
    				.Rows.Add(i, i * 7, i / 3)
    			Next
    		End With
    		DGV.DataSource = dt
    
    		For Each c As Control In Controls
    			If c.GetType Is GetType(ComboBox) Then
    				Dim cb As ComboBox = DirectCast(c, ComboBox)
    				With cb
    					.DataSource = dt.DefaultView.ToTable
    					.DisplayMember = "one"
    					.ValueMember = "three"
    					AddHandler .SelectedIndexChanged, AddressOf CB_SelectedIndexChanged
    				End With
    			End If
    		Next
    	End Sub
    	Private Sub CB_SelectedIndexChanged(sender As Object, e As EventArgs)
    		Dim cb As ComboBox = DirectCast(sender, ComboBox)
    		Label1.Text = cb.Name & " : " & cb.SelectedValue.ToString
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Thanks i used following two lines and modified to my needs.
    For Each c As Control In Controls
    If c.GetType Is GetType(ComboBox) Then

    Sunday, July 21, 2019 7:49 PM