我有一个带有两个 DataGridViewComboBoxColumns 的 DataGridView。我想使用第一列中的选定项目来触发第二列中每行项目的重新填充。

这是我到目前为止的代码。“addlInfoParentCat”标识第一列,currentRow.Cells.Item(1) 是我要重新填充的 DataGridViewComboBoxCell。ExtEventAdditionalInfoType 是我定义的包含字符串/值对的类型。

Private Sub dgvAdditionalInfo_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvAdditionalInfo.CellValueChanged
    Dim currentCell As DataGridViewCell
    currentCell = Me.dgvAdditionalInfo.CurrentCell
    If Not currentCell Is Nothing Then
        If currentCell.OwningColumn.DataPropertyName = "addlInfoParentCat" Then
            Dim parentTypeID As Integer = currentCell.Value

            Dim currentRow As DataGridViewRow = Me.dgvAdditionalInfo.CurrentRow
            Dim subtypeCell As DataGridViewComboBoxCell = currentRow.Cells.Item(1)

            Dim theChildren As New List(Of ExtEventAdditionalInfoType)

            theChildren = Custom_ExtEventAdditionalInfoType.GetChildrenOfThisParentOrderByTypeName(parentTypeID)
            subtypeCell.DataSource = Nothing
            subtypeCell.DataSource = theChildren
            subtypeCell.DisplayMember = "ExtEventAdditionalInfoTypeDescr"
            subtypeCell.ValueMember = "ID_ExtEventAdditionalInfoType"
        End If
    End If
End Sub

基本上我看到的是,第一次装订效果很好。当我在第一列中选​​择一个项目时,它会在第二列中正确填充该项目。我可以将行添加到 DataGridView 并重复该过程。

当我在第二列已经绑定后尝试更改第一列项目时,问题就出现了。我收到无穷无尽的对话框,其中包含以下内容:

系统参数异常:DataGridViewComboBoxCell 值无效。

知道为什么会发生这种情况吗?提前致谢!

更新 CodeByMoonlight 的建议似乎有效。

我在重新绑定之前清除了 DataGridViewComboBoxCell 的值:

....

            subtypeCell.DataSource = Nothing
            subtypeCell.Value = Nothing  'here's the change
            subtypeCell.DataSource = theChildren

....
有帮助吗?

解决方案

好吧,看起来一旦您重新修改第一个组合的值,您就会使用于填充第二个组合的绑定和数据源失效,从而导致所有错误。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top