在C#windows窗体中绑定组合框

在C#windows窗体中绑定组合框,c#,windows,forms,data-binding,combobox,C#,Windows,Forms,Data Binding,Combobox,我有两个组合框,它们都使用相同的数据源。 每当我更改其中一个组合框时,其他组合框都会更改为完全相同的值 似乎要解决这个问题,我需要使用数据绑定 我试过很多东西,但似乎都不管用。 我的组合框命名为comboBox1和comboBox2。 下面是我试图用来使combobox2独立于comboBox1运行的源代码。 任何帮助都将不胜感激 BindingSource bs = new BindingSource(this.claimTypeBindingSource, null); Bi

我有两个组合框,它们都使用相同的数据源。 每当我更改其中一个组合框时,其他组合框都会更改为完全相同的值

似乎要解决这个问题,我需要使用数据绑定

我试过很多东西,但似乎都不管用。 我的组合框命名为comboBox1和comboBox2。 下面是我试图用来使combobox2独立于comboBox1运行的源代码。 任何帮助都将不胜感激

    BindingSource bs = new BindingSource(this.claimTypeBindingSource, null);
    BindingContext bc = new BindingContext();
    comboBox2.BindingContext = bc;
    comboBox2.DataSource = bs.DataSource;
    comboBox2.DisplayMember = "ClaimType";
编辑 我刚刚将数据源分配从comboBox1中取出,并使用代码进行分配—代码现在看起来如下所示

    private void Form1_Activated(object sender, EventArgs e)
    {
        BindingSource bs1 = new BindingSource(this.claimTypeBindingSource, null);
        BindingContext bc1 = new BindingContext();
        comboBox1.BindingContext = bc1;
        comboBox1.DataSource = bs1.DataSource;
        comboBox1.DisplayMember = "ClaimType";

        BindingSource bs2 = new BindingSource(this.claimTypeBindingSource, null);
        BindingContext bc2 = new BindingContext();
        comboBox2.BindingContext = bc2;
        comboBox2.DataSource = bs2.DataSource;
        comboBox2.DisplayMember = "ClaimType";
    }
这并没有解决问题。 如果我更改了一个组合框,其他的也会更改。

我刚刚解决了它:-) 这是有效的——我已经把它投入到我的真实项目中,而且效果非常好

private void Form1_Activated(object sender, EventArgs e)
    {
        comboBox1.DataSource = new BindingSource(this.claimTypeBindingSource, null);
        comboBox1.DisplayMember = "ClaimType";

        comboBox2.DataSource = new BindingSource(this.claimTypeBindingSource, null);
        comboBox2.DisplayMember = "ClaimType";
    }

这是我用来填充两个组合框和 更改第一个组合框中的值不会更改其他组合框的值

Public Class Form1

    Dim data As New List(Of Product)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim bs1 As New BindingSource(data, Nothing)
        Dim bc1 As New BindingContext()
        ComboBox1.BindingContext = bc1
        ComboBox1.DataSource = bs1.DataSource
        ComboBox1.DisplayMember = "ProductName"

        Dim bs2 As New BindingSource(data, Nothing)
        Dim bc2 As New BindingContext()
        ComboBox2.BindingContext = bc2
        ComboBox2.DataSource = bs2.DataSource
        ComboBox2.DisplayMember = "ProductName"

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For intCounter As Integer = 1 To 10
            data.Add(New Product(intCounter, intCounter.ToString))
        Next

    End Sub
End Class


Public Class Product

    Public Property ProductID As Integer = 0
    Public Property ProductName As String = String.Empty

    Public Sub New(ID As Integer, Name As String)

        Me.ProductID = ID
        Me.ProductName = Name
    End Sub

End Class

您能提供更多的代码吗?例如,如何为第一个ComboBox设置相同的属性?在本例中,我刚刚将两个ComboBox从工具箱拖到表单设计器中,并将上面的代码添加到forms activated方法中。在combobox1中,我通过属性指定了一个数据源和DisplayMember,Combox2是使用上述代码指定的。我刚刚对代码进行了更改-不幸的是,这没有帮助。