2

我有一个带有两个 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 添加行并重复该过程。

当我在第二列已经绑定后尝试更改第一列项目时,问题就来了。我得到一连串的对话框,其中包含以下内容:

System.ArgumentException:DataGridViewComboBoxCell 值无效。

知道为什么会这样吗?提前致谢!

更新 CodeByMoonlight 的建议似乎有效。

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

....

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

....
4

1 回答 1

2

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

于 2009-09-16T00:56:31.577 回答