使用相同的数据源为多个组合框选择不同的值

时间:2018-05-17 09:43:19

标签: vb.net combobox

在这里和其他地方看起来像许多其他帖子一样的问题。 但到目前为止我尝试过的一切......都很有魅力。

让我解释一下:

在第一个片段中,我用一个空白行填充datagridview,并为同一个datagridview的每一列创建一个组合框。所有组合框都与相同的结合源结合。代码以这种方式编写,以允许创建与datagridview中的列一样多的组合框。列数可能每次都不同。

问题在于,当我更改第一个组合框的值时,所有其他组合都会更改为相同的值。这不是我想要达到的目标。现在这就是我到目前为止所尝试的内容。

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

上面的代码产生以下内容...... picture of comboboxes after load

当我从第一个组合框中的列表中选择一个不同的项目时,所有其他项目也会被更改。

所以我搜索了网页,发现相同的bindingsource可能是问题所在。 所以我改变了一些代码,每次都将组合框绑定到不同的绑定源。

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

我得到与CODE1相同的结果。组合框被创建得很好,但是当其中一个组合中的值发生变化时,所有组合都会相应地更改。

所以我认为也许可以创建像'cbc'这样的所有组合框(虽然它们有不同的名称(0,1,2,3)可能是问题。

此时我已经开放了任何建议。

  

根据fabio的建议,我改变了部分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 个答案:

答案 0 :(得分:0)

好的,我明白了。

由于' new bindingcontext'没有工作,我现在越来越相信重复绑定源会解决问题,我检查了绑定源重复的解决方案,发现应该更改以下行。所以......

Dim bs As new Bindingsource

应该成为:

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

因此,我现在的最终工作解决方案是:

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