0
votes

It seems as the same problem like in many other posts here and elsewhere. But everything I tried so far... failed glamorously.

Let me explain:

In the first snippet I populate datagridview with one blank row and create a combobox for each column of this same datagridview. All comboboxes are bind to the same bindingsource. The code is written in this way to allow creating as many comboboxes as there are columns in the datagridview. The number of columns may be different each time.

The problem is that when I change value of the first combobox all others are changed to the same value. And that is not what I'd like to achieve. Now here's what I've tried so far.

CODE1

Private Sub BT_paste_data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_paste_data.Click

        DGV.Rows.Add()
        Dim rect As Rectangle

        For i = 0 To DGV.Columns.Count - 1

     Dim cbc as combobox
            cbc = New ComboBox
            cbc.Name = i.ToString

            DGV.Controls.Add(cbc)
            cbc.Visible = True
            cbc.BringToFront()

            cbc.DataSource = datastringsBindingSource
            cbc.ValueMember = "id_data"
            cbc.DisplayMember = "data"
            cbc.SelectedItem = 9
            cbc.Text = "Don't add"
            cbc.FlatStyle = FlatStyle.Flat
            cbc.BackColor = SystemColors.Menu
            cbc.ForeColor = Color.FromArgb(64, 64, 64)
            cbc.Font = New Font(DGV.Font, FontHeight = 8.25)

            rect = DGV.GetCellDisplayRectangle(i, 0, False)

            cbc.Left = rect.Left
            cbc.Top = rect.Top

            DGV = 20

        Next
    End If
End Sub

The code above produces following... picture of comboboxes after load

and when I select a different item from the list in the first combobox all others are also changed.

So I searched the web and found that same bindingsource could be the problem. So I changed the code some to bind comboboxes to a different bindingsource each time.

CODE2

Private Sub BT_paste_data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_paste_data.Click

        DGV.Rows.Add()
        Dim rect As Rectangle

        For i = 0 To DGV.Columns.Count - 1

     Dim cbc as combobox
            cbc = New ComboBox
            cbc.Name = i.ToString

     DGV.Controls.Add(cbc)
            cbc.Visible = True
            cbc.BringToFront()

            'following two lines were added and the third changed accordingly
            Dim bs As New BindingSource
            bs = datastringsBindingSource
            cbc.DataSource = bs

            cbc.DataSource = bs
            cbc.ValueMember = "id_data"
            cbc.DisplayMember = "data"
            cbc.SelectedItem = 9
            cbc.Text = "Dont add"
            cbc.FlatStyle = FlatStyle.Flat
            cbc.BackColor = SystemColors.Menu
            cbc.ForeColor = Color.FromArgb(64, 64, 64)
            cbc.Font = New Font(DGV.Font, FontHeight = 8.25)

            rect = DGV.GetCellDisplayRectangle(i, 0, False)

            cbc.Left = rect.Left
            cbc.Top = rect.Top

            DGV = 20

        Next
    End If

End Sub

I get same result as with CODE1. Comboboxes are created fine but when value in one of them is changed all of them get changed accordingly.

So I thought that perhaps creating all comboboxes like 'cbc' (although they have different names (0,1,2,3) could be the problem.

At this point I'm opened for any suggestions.

following the suggestion from fabio I changed a part of the CODE2:

Private Sub BT_paste_data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_paste_data.Click

    DGV.Rows.Add()
    Dim rect As Rectangle

    For i = 0 To DGV.Columns.Count - 1

 Dim cbc as combobox
        cbc = New ComboBox
        cbc.Name = i.ToString

 DGV.Controls.Add(cbc)
        cbc.Visible = True
        cbc.BringToFront()

        'following two lines were added and the third changed accordingly
        Dim bs As New BindingSource
        bs = datastringsBindingSource

        'next line was suggested by fabio but the result is still the same
        cbc.BindingContext = New BindingContext()
        cbc.DataSource = bs
        cbc.ValueMember = "id_data"
        cbc.DisplayMember = "data"
        cbc.SelectedItem = 9
        cbc.Text = "Dont add"
        cbc.FlatStyle = FlatStyle.Flat
        cbc.BackColor = SystemColors.Menu
        cbc.ForeColor = Color.FromArgb(64, 64, 64)
        cbc.Font = New Font(DGV.Font, FontHeight = 8.25)

        rect = DGV.GetCellDisplayRectangle(i, 0, False)

        cbc.Left = rect.Left
        cbc.Top = rect.Top

        DGV = 20

    Next
End If

End Sub
1
Use cbc.BindingContext = new BindingContext(); before setting datasourceFabio
thank you... I tried it but the result is still the same. I changed the original post and included your suggestionnobsvalo

1 Answers

0
votes

OK I got it.

Since 'new bindingcontext' didn't work and I was now more and more convinced that duplicating bindingsource would solve the problem, I checked for solutions on bindingsource duplication and found out following line should be changed. So...

Dim bs As new Bindingsource

should become:

Dim bs As new Bindingsource(datastringsBindingSource.Datasource, datastringsBindingSource.DataMember)

Therefore my final working solution is for now:

Private Sub BT_paste_data_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_paste_data.Click

DGV.Rows.Add()
Dim rect As Rectangle

For i = 0 To DGV.Columns.Count - 1

    Dim cbc as combobox
    cbc = New ComboBox
    cbc.Name = i.ToString

    DGV.Controls.Add(cbc)
    cbc.Visible = True
    cbc.BringToFront()

    'here is the line that was changed
    Dim bs As new Bindingsource(datastringsBindingSource.Datasource, datastringsBindingSource.DataMember)
    bs = datastringsBindingSource

    'next line was suggested by fabio but the result is still the same
    'cbc.BindingContext = New BindingContext() - sorry fabio, this line seems to be irelevant
    cbc.DataSource = bs
    cbc.ValueMember = "id_data"
    cbc.DisplayMember = "data"
    cbc.SelectedItem = 9
    cbc.Text = "Dont add"
    cbc.FlatStyle = FlatStyle.Flat
    cbc.BackColor = SystemColors.Menu
    cbc.ForeColor = Color.FromArgb(64, 64, 64)
    cbc.Font = New Font(DGV.Font, FontHeight = 8.25)

    rect = DGV.GetCellDisplayRectangle(i, 0, False)

    cbc.Left = rect.Left
    cbc.Top = rect.Top

    DGV = 20

Next

End Sub