datagrid error: Exception has been thrown by the target of an invocation

datagrid error: Exception has been thrown by the target of an invocation

我正在开发 AutoCAD 插件。以下三个函数来自 AutoCAD 的课程,由 Jerry Winters 编写。我正在使用这些函数来获取我需要的数据并将其传递到数据网格中。

    Function SelectAllBlocks(dbIn As Database) As ObjectIdCollection
    Dim retIDs As New ObjectIdCollection
    Using myTrans As Transaction = dbIn.TransactionManager.StartTransaction
        Dim myBT As BlockTable = dbIn.BlockTableId.GetObject(OpenMode.ForRead)
        For Each btrID As ObjectId In myBT
            Dim myBTR As BlockTableRecord = btrID.GetObject(OpenMode.ForRead)
            For Each BRefID As ObjectId In myBTR.GetBlockReferenceIds(True, False)
                retIDs.Add(BRefID)
            Next
        Next
    End Using
    Return retIDs
End Function
Function GetBlockProperties(ObjID As ObjectId) As Dictionary(Of String, Object)
    Dim myD As New Dictionary(Of String, Object)
    Using myTrans As Transaction =
    ObjID.Database.TransactionManager.StartTransaction
        Dim myEnt As BlockReference =
        ObjID.GetObject(OpenMode.ForRead)
        For Each myPInfo As Reflection.PropertyInfo In GetType(BlockReference).GetProperties
            myD.Add(myPInfo.Name, myPInfo.GetValue(myEnt))
        Next
    End Using
    Return myD
End Function
Function GetBTRProperties(ObjID As ObjectId) As Dictionary(Of String, Object)
    Dim myD As New Dictionary(Of String, Object)
    Using myTrans As Transaction =
    ObjID.Database.TransactionManager.StartTransaction
        Dim myEnt As BlockTableRecord =
        ObjID.GetObject(OpenMode.ForRead)
        For Each myPInfo As System.Reflection.PropertyInfo In GetType(BlockTableRecord).GetProperties
            myD.Add(myPInfo.Name, myPInfo.GetValue(myEnt))
        Next
    End Using
    Return myD
End Function

我的代码试图根据自己的需要重写课程中的某些 material,将一些信息加载到数据网格中,但抛出 "Exception has been thrown by the target of an invocation" 错误。

    Private Sub cmdBlocks_Click(sender As Object, e As EventArgs) Handles cmdBlocks.Click
    Dim tbl As New System.Data.DataTable
    ' Create four typed columns in the DataTable.
    tbl.Columns.Add("User name", GetType(String))
    tbl.Columns.Add("Full name", GetType(String))
    tbl.Columns.Add("Time", GetType(String))
    tbl.Columns.Add("name", GetType(String))
    tbl.Columns.Add("Block handle", GetType(String))
    tbl.Columns.Add("Block name", GetType(String))
    tbl.Columns.Add("Location X", GetType(String))
    tbl.Columns.Add("Location Y", GetType(String))
    tbl.Columns.Add("Location Z", GetType(String))
    dgvBlocks.DataSource = tbl
    Dim myDB As Database = HostApplicationServices.WorkingDatabase,
        myIDs As ObjectIdCollection = SelectAllBlocks(myDB)
    Using myTrans As Transaction = myDB.TransactionManager.StartTransaction
        For Each myBID As ObjectId In myIDs
            Dim blockProps As Dictionary(Of String, Object) = GetBlockProperties(myBID)
            Dim blockProps2 As Dictionary(Of String, Object) = GetBTRProperties(blockProps("DynamicBlockTableRecord"))
            Dim blockAtts As Dictionary(Of String, Dictionary(Of String, String)) = GetBlockAttributes(myBID)
            Dim username As String = My.Computer.Name,
                filename As String = HostApplicationServices.WorkingDatabase.Filename,
                time As String = DateTime.UtcNow,
                Name As String = blockProps2("Name"),
                BlockHandle As String = blockProps("Handle").ToString,
                BlockName As String = blockProps("BlockName").ToString,
                location As Point3d = blockProps("Position"),
                locX As String = location.X.ToString,
                locY As String = location.Y.ToString,
                locZ As String = location.Z.ToString
            tbl.Rows.Add(username, filename, time, Name, BlockHandle, BlockName, locX, locY, locZ)
        Next
        myTrans.Abort()
    End Using
End Sub

eLockViolation”异常通常发生在您尝试从无模式窗体或调色板(也是无模式)修改数据库时。

这是锁定它的方法:

Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
    ' do your code here...
End Using

通常你调用它一次,这意味着每个操作一个锁来更改数据库,即使你有多个事务,但你可能会考虑每个事务一个锁以使其更容易实现(尽管性能成本更高)。