0

我有一个 DataGridView,我已将对象列表设置为 DataSource。(我在 VS 2005 中使用 VB。)我通过创建 AssetIdentifier 类型的数据源并将该数据源拖到我的表单上来创建 DataGridView。

当组合框或另一个 DataGridView 中的选择发生更改时,我想更新 DataGridView。(下面我正在考虑单击另一个 DataGridView。)以下工作:

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

在这种情况下,我一直都知道assetIdList 至少有一个元素。我会更新列表,并将 DataGridView 的数据源重置为该列表,一切都很好。

当我将此应用于另一种情况时,我无法保证列表中至少有一个元素,只要列表中至少有一个元素,一切都会正常工作,但如果列表为空, DataGridView 多次抛出 System.IndexOutOfRangeException。如果我从非零元素数变为零,DataGridView 中的行不会消失。

我尝试了一种解决方法,即删除所有元素,添加一个“虚拟”元素,然后将列表重新绑定到控件,但它仍然不起作用。

此外,在所有这些异常之后,当我将鼠标悬停在 DataGridView 中的单元格上时,我会遇到其他类似的异常。

几个小时以来,我一直试图追踪这种行为。有什么见解吗?谢谢!

如果需要,将很乐意添加更多信息。

更新: AssetIdentifier 的一些成员是“Nothing”,但我在构造函数中修复了它,异常仍然发生。

4

1 回答 1

0

重构代码,它可以工作......

于 2009-09-08T23:36:53.280 回答