.Net:在windows应用程序中填充表单上多个组合框的最佳技术?

.Net:在windows应用程序中填充表单上多个组合框的最佳技术?,.net,winforms,combobox,fill,.net,Winforms,Combobox,Fill,假设您有一个带有5个组合框的赢单 有人告诉我,你必须有5个数据表,以填补他们一个接一个。这似乎不太合乎逻辑,我认为这会产生难看的代码 如何用存储在数据库中的数据填充它们 我个人认为WinForms数据绑定很痛苦,所以我用“for”循环手动绑定。我只需循环一个数据集,并将元素添加到循环中的每个组合框中 注意:我从来没有想过在WPF中这样做——它的数据绑定机制要好得多…就个人而言,我认为WinForms数据绑定很痛苦,所以我使用“for”循环手动执行。我只需循环一个数据集,并将元素添加到循环中的每个

假设您有一个带有5个组合框的赢单

有人告诉我,你必须有5个数据表,以填补他们一个接一个。这似乎不太合乎逻辑,我认为这会产生难看的代码


如何用存储在数据库中的数据填充它们

我个人认为WinForms数据绑定很痛苦,所以我用“for”循环手动绑定。我只需循环一个数据集,并将元素添加到循环中的每个组合框中


注意:我从来没有想过在WPF中这样做——它的数据绑定机制要好得多…

就个人而言,我认为WinForms数据绑定很痛苦,所以我使用“for”循环手动执行。我只需循环一个数据集,并将元素添加到循环中的每个组合框中


注意:我从未想过在WPF中这样做——它的数据绑定机制要好得多…

我使用的类类似于:

Public Class ComboboxBinder(Of TKey, TValue)
    Inherits List(Of KeyValuePair(Of TKey, TValue))

    Public Sub New()
    End Sub

    Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
        MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
    End Sub

    Public Sub Bind(ByVal control As ComboBox)
        control.DisplayMember = "Value"
        control.ValueMember = "Key"
        control.DataSource = Me
    End Sub

End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim binder As New ComboboxBinder(Of Guid, String)
    binder.Add(Guid.NewGuid, "Item 1")
    binder.Add(Guid.NewGuid, "Item 2")
    binder.Add(Guid.NewGuid, "Item 3")
    binder.Add(Guid.NewGuid, "Item 4")

    binder.Bind(ComboBox1)

End Sub
然后,要使用它,您可以放置如下内容:

Public Class ComboboxBinder(Of TKey, TValue)
    Inherits List(Of KeyValuePair(Of TKey, TValue))

    Public Sub New()
    End Sub

    Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
        MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
    End Sub

    Public Sub Bind(ByVal control As ComboBox)
        control.DisplayMember = "Value"
        control.ValueMember = "Key"
        control.DataSource = Me
    End Sub

End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim binder As New ComboboxBinder(Of Guid, String)
    binder.Add(Guid.NewGuid, "Item 1")
    binder.Add(Guid.NewGuid, "Item 2")
    binder.Add(Guid.NewGuid, "Item 3")
    binder.Add(Guid.NewGuid, "Item 4")

    binder.Bind(ComboBox1)

End Sub
Winforms对于绑定来说是一个难题,但是这个解决方案对于我们公司来说已经足够好了。
另外值得注意的是,您不能绑定到
字典,它必须是
IList
IListSource
,因此为什么
组合框
活页夹是一个
列表

,我使用的类看起来像这样:

Public Class ComboboxBinder(Of TKey, TValue)
    Inherits List(Of KeyValuePair(Of TKey, TValue))

    Public Sub New()
    End Sub

    Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
        MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
    End Sub

    Public Sub Bind(ByVal control As ComboBox)
        control.DisplayMember = "Value"
        control.ValueMember = "Key"
        control.DataSource = Me
    End Sub

End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim binder As New ComboboxBinder(Of Guid, String)
    binder.Add(Guid.NewGuid, "Item 1")
    binder.Add(Guid.NewGuid, "Item 2")
    binder.Add(Guid.NewGuid, "Item 3")
    binder.Add(Guid.NewGuid, "Item 4")

    binder.Bind(ComboBox1)

End Sub
然后,要使用它,您可以放置如下内容:

Public Class ComboboxBinder(Of TKey, TValue)
    Inherits List(Of KeyValuePair(Of TKey, TValue))

    Public Sub New()
    End Sub

    Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue)
        MyBase.Add(New KeyValuePair(Of TKey, TValue)(key, value))
    End Sub

    Public Sub Bind(ByVal control As ComboBox)
        control.DisplayMember = "Value"
        control.ValueMember = "Key"
        control.DataSource = Me
    End Sub

End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim binder As New ComboboxBinder(Of Guid, String)
    binder.Add(Guid.NewGuid, "Item 1")
    binder.Add(Guid.NewGuid, "Item 2")
    binder.Add(Guid.NewGuid, "Item 3")
    binder.Add(Guid.NewGuid, "Item 4")

    binder.Bind(ComboBox1)

End Sub
Winforms对于绑定来说是一个难题,但是这个解决方案对于我们公司来说已经足够好了。 另外值得注意的是,您不能绑定到
字典,它必须是
IList
IListSource
,因此为什么
组合框
活页夹是一个
列表

,它可以成为Windows窗体中您最好的朋友

然后可以指定该属性。在设计时绑定了es,即将每个属性设置为
BindingSource
的实例后,现在只需填充
BindingSource

请参阅我的答案,以查看使用
BindingSource
类的Windows窗体数据绑定的大致演练

对于每个组合框的表策略,这可能很有用,因为每个层次结构都有一个表。在这里,我们更可能讨论对象关系持久化策略,这可能是一个很长的过程。简言之,有关更多详细信息,请参阅NHibernate参考文档和。您可能更关心
三种策略

现在我知道了,你在问题中没有提到NHibernate等。我的目标是简单地向您展示一些最流行的实体持久性策略,您的问题会间接地询问这些策略。

在Windows窗体中,它们可能是您最好的朋友

然后可以指定该属性。在设计时绑定了es,即将每个属性设置为
BindingSource
的实例后,现在只需填充
BindingSource

请参阅我的答案,以查看使用
BindingSource
类的Windows窗体数据绑定的大致演练

对于每个组合框的表策略,这可能很有用,因为每个层次结构都有一个表。在这里,我们更可能讨论对象关系持久化策略,这可能是一个很长的过程。简言之,有关更多详细信息,请参阅NHibernate参考文档和。您可能更关心
三种策略


现在我知道了,你在问题中没有提到NHibernate等。我的目标是简单地向您展示一些最流行的实体持久性策略,这是您的问题间接提出的。

@nportelli:没错,与IList或IList一样多的BindingList都可以用作数据源。@nportelli:没错,与IList或IList一样多的BindingList都可以用作数据源。