none
How to unlink mirroring of 2 combo boxes bound to the same data source RRS feed

  • Question

  • I am using VS2015 with visual basic code. I have 2 combo boxes that are bound to the same data Source that the user chooses values from the drop down. However, when a value is chosen for either one, the other automatically displays the same thing. I have tried setting the selected index value to -1 for the "other" combo box but it doesn't work. How can I unlink them so that choosing a value from one doesn't affect the other?

    Thanks.

    Wednesday, May 4, 2016 4:58 PM

Answers

  • It depends upon what you are using for a datasource.

    For any datasource you could simply use separate BindingSources to 'connect' each combobox to the source.

    If the source was a DataTable without a BindingSource then you could use a separate DataView for each combobox.

    Wednesday, May 4, 2016 10:40 PM
  • How can I unlink them so that choosing a value from one doesn't affect the other?

    Bind them to different data sources. The two sources can contain the same data. That might not be suitable for whatever other processing you are using the comboboxes for.

    Or, don't use data binding.  You should not be using binding if you don't want the features that binding provides. 

    Wednesday, May 4, 2016 9:54 PM
  • I figured out how to create a separate datasource using the code below. Now when I choose a value from the other dropbox it doesn't affect the second one. However, that created a new problem. How do I fill the second drop down box since the new data table I created doesn't "physically" exist in the dataset.

    Dim QryStaffData_1 As DataTable
    QryStaffData_1 = AnnualSurveyDataSet.Tables("QryStaffData").Copy
    StaffSelectComboBox2.DataSource = QryStaffData_1
    StaffSelectComboBox2.DisplayMember = "StaffName"
    StaffSelectComboBox2.ValueMember = "EmpID"
    StaffSelectComboBox2.SelectedIndex = -1

    Thursday, May 5, 2016 1:48 PM
  • Thursday, May 5, 2016 2:33 PM
  • As you are using a DataTable you can use separate views as I suggested above.

    Have a look at the following which shows 10 combobox all showing the same data but independently of each other.

    Public Class Form1
       Dim DT As New DataTable
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          Height = 400
          DT.Columns.Add("Column1", GetType(Integer))
          For count As Integer = 1 To 10
             Dim Row As DataRow = DT.NewRow
             Row.Item("Column1") = count
             DT.Rows.Add(Row)
          Next
          For count As Integer = 1 To 10
             Dim cmbo As New ComboBox With {.Location = New Point(20, count * 30)}
             Dim view As New DataView(DT)
             cmbo.DataSource = view
             cmbo.DisplayMember = "Column1"
             Controls.Add(cmbo)
          Next
       End Sub
    End Class


    Thursday, May 5, 2016 2:45 PM

All replies

  • How can I unlink them so that choosing a value from one doesn't affect the other?

    Bind them to different data sources. The two sources can contain the same data. That might not be suitable for whatever other processing you are using the comboboxes for.

    Or, don't use data binding.  You should not be using binding if you don't want the features that binding provides. 

    Wednesday, May 4, 2016 9:54 PM
  • It depends upon what you are using for a datasource.

    For any datasource you could simply use separate BindingSources to 'connect' each combobox to the source.

    If the source was a DataTable without a BindingSource then you could use a separate DataView for each combobox.

    Wednesday, May 4, 2016 10:40 PM
  • A couple of things:

    1- I have to bind them to a datasource because I need both the description field (Display Value) and the index key (value member) as part of it.

    2 - I thought about having separate data sources but in all I need 10 dropdowns with the same data so was hoping to avoid having to set up 10 individual data sources.

    Thanks.  

    Thursday, May 5, 2016 12:24 PM
  • I figured out how to create a separate datasource using the code below. Now when I choose a value from the other dropbox it doesn't affect the second one. However, that created a new problem. How do I fill the second drop down box since the new data table I created doesn't "physically" exist in the dataset.

    Dim QryStaffData_1 As DataTable
    QryStaffData_1 = AnnualSurveyDataSet.Tables("QryStaffData").Copy
    StaffSelectComboBox2.DataSource = QryStaffData_1
    StaffSelectComboBox2.DisplayMember = "StaffName"
    StaffSelectComboBox2.ValueMember = "EmpID"
    StaffSelectComboBox2.SelectedIndex = -1

    Thursday, May 5, 2016 1:48 PM
  • Thursday, May 5, 2016 2:33 PM
  • As you are using a DataTable you can use separate views as I suggested above.

    Have a look at the following which shows 10 combobox all showing the same data but independently of each other.

    Public Class Form1
       Dim DT As New DataTable
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          Height = 400
          DT.Columns.Add("Column1", GetType(Integer))
          For count As Integer = 1 To 10
             Dim Row As DataRow = DT.NewRow
             Row.Item("Column1") = count
             DT.Rows.Add(Row)
          Next
          For count As Integer = 1 To 10
             Dim cmbo As New ComboBox With {.Location = New Point(20, count * 30)}
             Dim view As New DataView(DT)
             cmbo.DataSource = view
             cmbo.DisplayMember = "Column1"
             Controls.Add(cmbo)
          Next
       End Sub
    End Class


    Thursday, May 5, 2016 2:45 PM