这是一个模拟示例,我在其中模拟数据以模拟从数据库加载。当用户选择一个项目时,有一个标签显示更改项目的 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