我在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 –