我有一个具有两个DataGridView ComboBoxColumn的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值无效。
知道为什么会这样吗?事先谢谢!
更新
月光的建议似乎奏效了。
在重新绑定之前,我清除了DataGridViewComboBoxCell的值:
....
subtypeCell.DataSource = Nothing
subtypeCell.Value = Nothing 'here's the change
subtypeCell.DataSource = theChildren
....