views:

533

answers:

1

I have a DataGridView to which I've set a list of objects to the DataSource. (I'm in VS 2005 using VB.) I created the DataGridView by creating a Data Source of type AssetIdentifier and dragging that Data Source onto my form.

I want to update the DataGridView when the selection in either a combo box or another DataGridView changes. (Below I'm considering a click in another DataGridView.) The following works:

Public Class dlgShowAssets

    ' class member variable

    Private assetIdList As List(Of AssetIdentifier)

    ' pertinent subs and functions

    Private Sub RefreshAssetIdentifierDataGridView()
        AssetIdentifierDataGridView.DataSource = assetIdList
    End Sub

    Private Sub AssetDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles AssetDataGridView.CellClick     
        assetIdList = RepopulateTheList(id)
        Me.RefreshAssetIdentifierDataGridView()
    End Sub

End Class

In this case, I always knew that assetIdList would have at least one element. I'd update the list, and reset the data source of the DataGridView to that list, and all was well.

When I applied this to another situation, for which I couldn't guarantee that the list would have at least one element, things would work fine as long as I had at least one element in the list, but if the list became empty, the DataGridView threw System.IndexOutOfRangeException a number of times. The rows in the DataGridView would not go away if I went from a non-zero number of elements to zero.

I tried a workaround, which was to remove all of the elements, add one "dummy" element, and then re-bind the list to the control, and it still didn't work.

Also, following all of those exceptions, I'd get other similar exceptions when I hovered over the cells in the DataGridView.

I've been trying to track down this behavior for a few hours. Any insights? Thanks!

Will be happy to add more info if needed.

UPDATE: Some of the members of AssetIdentifier were "Nothing" but I fixed that in the constructor, and the exceptions still occur.

A: 

Refactored code and it works ...

John at CashCommons
Would be great to know how you fixed it since I've got the same problem...
Stecy
I'll see if I can track down the changes.
John at CashCommons