【问题标题】:Visual basic Populate DataGridView with ComboBoxColumnVisual basic 使用 ComboBoxColumn 填充 DataGridView
【发布时间】:2016-09-08 17:58:35
【问题描述】:

假设我有一个带有 ComboBoxColumn 的 DataGridView。我为 ComboBoxes 添加项目如下:

Dim colors = New Dictionary(Of String, String)()
colors("10") = "Red"
colors("20") = "Blue"
colors("30") = "Green"
colors("40") = "Yellow"
ComboBox1.DataSource = New BindingSource(colors, Nothing)
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "Key"

然后当程序启动时,必须从数据库中填充 DataGridView,并且 ComboBoxes 应该根据数据库显示相应的选定项。

在数据库中,值将是 10203040,并且组合框应显示红色蓝色绿色黄色

问题:如何填充 DATAGRIDVIEW 以使每个组合框单元格都有相应的值??

【问题讨论】:

  • 好的。那么问题是什么?
  • 显然 - 如何正确填充 te DGV
  • 不明显。一个问题总是有一个 ?

标签: vb.net datagridview combobox


【解决方案1】:

这是一个模拟示例,我在其中模拟数据以模拟从数据库加载。当用户选择一个项目时,有一个标签显示更改项目的 ValueMember,以便您可以在需要时对其进行操作。希望这是有趣的。

''' <summary>
''' There are two columns in the DataGridView, both
''' have the DataProperty set to ParentId for the
''' first column a ComboBox, ChildName for the 
''' second column a text box.
''' </summary>
Public Class Form1
    WithEvents bsData1 As New BindingSource
    WithEvents bsData2 As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        DataGridView1.AutoGenerateColumns = False
        bsData2.DataSource = GetParentTable()

        Column1.DisplayMember = "ParentName"
        Column1.ValueMember = "ParentID"
        Column1.DataSource = bsData2
        Column1.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing

        bsData1.DataSource = GetChildTable()
        DataGridView1.DataSource = bsData1

        UpdateLabel()
    End Sub
    ''' <summary>
    ''' Mocked data, in an app would come from a database table
    ''' </summary>
    ''' <returns></returns>
    Private Function GetParentTable() As DataTable
        Dim dt As New DataTable

        With dt.Columns
            .Add("ParentID", GetType(Integer))
            .Add("ParentName", GetType(String))
        End With

        With dt.Rows
            .Add(10, "Red")
            .Add(20, "Blue")
            .Add(30, "Green")
        End With

        dt.AcceptChanges()

        Return dt
    End Function
    ''' <summary>
    ''' Mocked data, in an app would come from a database table
    ''' </summary>
    ''' <returns></returns>
    Private Function GetChildTable() As DataTable
        Dim dt As New DataTable

        With dt.Columns
            .Add("ChildID", GetType(Integer))
            .Add("ParentID", GetType(Integer))
            .Add("ChildName", GetType(String))
        End With

        With dt.Rows
            .Add(1, 30, "Child 1")
            .Add(2, 20, "Child 2")
            .Add(3, 10, "Child 3")
        End With

        dt.AcceptChanges()

        Return dt

    End Function
    Private Sub DataGridView1_CellEndEdit(
        sender As Object,
        e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

        UpdateLabel()

    End Sub
    Private Sub bsData1_PositionChanged(sender As Object, e As EventArgs) Handles bsData1.PositionChanged
        UpdateLabel()
    End Sub
    Private Sub UpdateLabel()
        If bsData1.DataSource IsNot Nothing Then
            If bsData1.Current IsNot Nothing Then
                Label1.Text = CType(bsData1.Current, DataRowView).Row.Field(Of Integer)("ParentID").ToString
            End If
        End If
    End Sub
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多