views:

650

answers:

1

I have a DataGridView with two DataGridViewComboBoxColumns. I want to use the selected item in the first column to trigger a re-population of the items in the second column, on a per-row basis.

Here's the code I have so far. "addlInfoParentCat" identifies the first column, and currentRow.Cells.Item(1) is the DataGridViewComboBoxCell that I want to re-populate. ExtEventAdditionalInfoType is a type I defined that contains the 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 what I see is that the binding works great the first time around. When I select a item in the first column, it populates the items correctly in the second. I can add rows to the DataGridView and repeat the process.

The problem comes when I try to change the first-column item after the second column has already been bound. I get an endless string of dialog boxes with the following:

System.ArgumentException: DataGridViewComboBoxCell value is not valid.

Any idea why this is happening? Thanks in advance!

UPDATE CodeByMoonlight's suggestion appears to work.

I clear the DataGridViewComboBoxCell's value before re-binding:

....

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

....
+1  A: 

Well, it looks like as soon as you remodify the first combo's value, you're invalidating the binding and datasource you used to populate the second combo, causing all the errors.

CodeByMoonlight
Thanks for your answer CodeByMoonlight. I'm afraid I don't follow. Each time I change the selection in the first column, I get a fresh list of theChildren, and re-bind the second combo with this new list. The binding is invalid, but then I reset it, don't I? Or am I not doing the rebinding correctly?
John at CashCommons
I think the key is that the binding is briefly invalid, not that you then correct it. I think if you clear the second combo's value prior to loading the new datasource, it should be fine. However i'm posting from Opera Mini so i can't test this right now.
CodeByMoonlight
That fixed it. Thank you!
John at CashCommons