Error while trying to restore DataSource data in DataGridViewComboBoxCell?
I have a DataGridView with two DataGridViewComboBoxColumns. I want to use the selected item in the first column to trigger repopulation of the items in the second column in every row.
Here is the code I have. "addlInfoParentCat" identifies the first column, and currentRow.Cells.Item (1) is the DataGridViewComboBoxCell that I want to repopulate. ExtEventAdditionalInfoType is type I that contains string / value pairs.
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
Basically I can see that the binding works fine the first time. When I select an item in the first column, it fills in the items in the second correctly. I can add rows to the DataGridView and repeat the process.
The problem comes when I try to change the element of the first column after the second column is already linked. I am getting an endless string of dialog boxes with the following:
System.ArgumentException: The DataGridViewComboBoxCell value is not valid.
Any idea why this is happening? Thanks in advance!
UPDATE The CodeByMoonlight suggestion appears to be working.
I clear the DataGridViewComboBoxCell value before re-binding:
....
subtypeCell.DataSource = Nothing
subtypeCell.Value = Nothing 'here the change
subtypeCell.DataSource = theChildren
....
source to share