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
cbc.BindingContext = new BindingContext();
before setting datasource – Fabio