-1

I have a form that upon pressing a button creates a panel inside a FlowLayoutPanel. the panel does contain other control objects as can be seen from the pic. The problem that controls in different panels are linked together ,when in choose in combobox all others change to the same

This before any selection

1

After selection :

2

Shouldn't they be bound by their parent control which in this case will be the panel ?

vimuth
  • 5,064
  • 33
  • 79
  • 116
Bassem
  • 3
  • 2
  • Probably an idea you update your question with code sample of how your adding these panels and setting up the binding for each panel – Hursey Aug 20 '22 at 20:20
  • If it didn't work then you did it wrong. If we can't see what you did then we can't see what's wrong with it. – user18387401 Aug 21 '22 at 02:23
  • 1
    Do each of these `Panels` contain the same set of child controls? If so then you should not be using `Panels` at all. You should design a user control first, then add instances of that user control to your `FlowLayoutPanel`. User controls are designed the same way forms are, but then used the same way controls are. – user18387401 Aug 21 '22 at 02:24
  • 1
    My first guess regarding the issue is that you are binding all of the controls to the same source. If you use a different `BindingSource` for each control then I think you should be able to make independent selections in each control, because there will be a different `CurrencyManager` for each control. – user18387401 Aug 21 '22 at 02:28

1 Answers1

1

I just tested what happens when you bind two ComboBox controls to the same DataTable directly and when you bind two ComboBox controls to different BindingSources that are bound to the same DataTable. I added four ComboBoxes and two BindingSources to a form and then added this code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table1 As New DataTable
    Dim table2 As New DataTable

    With table1.Columns
        .Add("Id", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table2.Columns
        .Add("Id", GetType(Integer))
        .Add("Name", GetType(String))
    End With

    With table1.Rows
        .Add(1, "One")
        .Add(2, "Two")
    End With

    With table2.Rows
        .Add(1, "First")
        .Add(2, "Second")
    End With

    BindingSource1.DataSource = table1
    BindingSource2.DataSource = table1

    With ComboBox1
        .DisplayMember = "Name"
        .ValueMember = "Id"
        .DataSource = BindingSource1
    End With

    With ComboBox2
        .DisplayMember = "Name"
        .ValueMember = "Id"
        .DataSource = BindingSource2
    End With

    With ComboBox3
        .DisplayMember = "Name"
        .ValueMember = "Id"
        .DataSource = table2
    End With

    With ComboBox4
        .DisplayMember = "Name"
        .ValueMember = "Id"
        .DataSource = table2
    End With
End Sub

When I ran the project, I saw "One" displayed in the first two ComboBoxes and "First" displayed in the last two, as expected. I was able to make a selection in either of the first two ComboBoxes without affecting the other, while making a selection in either of the last two ComboBoxes did affect the other. This is almost certainly a demonstration of the issue you're seeing and the solution to said issue. Using the same original data source is fine but you should pretty much always use a BindingSource to bind to your control(s).

If you create a user control, as I recommend you do for groups of child controls that you want to reuse, then the BindingSource object(s) can be added in the designer and used internally. You can just add a property to the control for the data source.

user18387401
  • 2,514
  • 1
  • 3
  • 8
  • Hi. Thanks for your support guys .you are right user18387401 the problem came from all comboboxes common datasource. adding ComboBox1.BindingContext = New BindingContext() during its creation was enough to solve the problem – Bassem Aug 21 '22 at 16:07
  • Or you could have used a `BindingSource`, as you're supposed to and as I instructed. – user18387401 Aug 22 '22 at 12:19