我在VB.Net(Winforms)中使用以下代碼來簡單地遍歷DataGridView
並隱藏不需要的行。VB.Net異常已被調用的目標拋出
Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
For Each row In Incident_Persons_List.Rows
If Incident_Persons_List.Rows(CInt(row)).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(CInt(row)).Visible = True
Else
Incident_Persons_List.Rows(CInt(row)).Visible = False
End If
Next
End Sub
當調試器獲取到IF
聲明的第一行,我得到以下錯誤:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
我一直在嘗試一切我能想到的明白這是爲什麼。我查過這個錯誤,但是當拋出這個異常時,每個人似乎都有完全不同的問題。
這與我如何做比較有關嗎?
更新1
- 我已刪除了
For Each
與For i = 0 to Incident_Persons_list.Rows.Count
取而代之 - 我已刪除了
Cint
指令 - 的
Try/Catch
如透露,被拋出的實際的例外是:
Row associated with the currency manager's position cannot be made invisible.
更新2
一切現在與下面的代碼正常工作:
Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
Try
For i = 0 To Incident_Persons_List.Rows.Count - 1
If Incident_Persons_List.Rows(i).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(i).Visible = True
Else
'Your code that will throw the Exception
Incident_Persons_List.CurrentCell = Nothing
Incident_Persons_List.Rows(i).Visible = False
End If
Next
Catch ex As TargetInvocationException
'We only catch this one, so you can catch other exception later on
'We get the inner exception because ex is not helpfull
Dim iEX = ex.InnerException
Debug.Print(iEX.Message)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub
感謝您的幫助!
顯示** complete **異常的詳細信息,InnerException對於找出哪裏出錯是至關重要的。 –
沒有內部異常,這就是我面臨 – SilverShotBee
包裹在一個try/catch結構代碼塊的問題,然後檢查InnerExcpetion –