2016-06-28 46 views
1

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

  1. 我已删除了For EachFor i = 0 to Incident_Persons_list.Rows.Count取而代之
  2. 我已删除了Cint指令
  3. 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 

感谢您的帮助!

+0

显示** complete **异常的详细信息,InnerException对于找出哪里出错是至关重要的。 –

+0

没有内部异常,这就是我面临 – SilverShotBee

+0

包裹在一个try/catch结构代码块的问题,然后检查InnerExcpetion –

回答

2

TargetInvocationException

The exception that is thrown by methods invoked through reflection

如何找出发生了什么事情(因为该异常是不是真的有帮助)?

必须surroung用Try/Catch结构调用块,然后检查InnerException抓:

Try 
    'Your code that will throw the Exception 
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 
    'Now you can do some stuff to handle your exception 
Catch ex As Exception 
    'Here you catch other kinds of Exceptions that could occur in your code, if you want to... 
End Try 

,并根据上设置InnerException,你现在可以纠正你的代码。

+0

投票以此为答案,因为虽然这是没有明确的>答<,这是一个奇妙的方法,帮助我找到问题的途径原因没有额外的帮助 – SilverShotBee

+2

作为一个建议,你应该在每个子使用一个try/catch处理绑定到UI操作的事件。由于用户操作是应用程序中最不可预知的事情,这使得它们成为意外行为的良好选择。另请注意,InnerException并不特定于TargetInvocationException。当您在“第一级”找不到足够的信息时,请务必查看此属性。 –

2

由于您的行变量是Incident_Persons_List.Rows的枚举元素,而不是集合中元素的索引,我认为您应该替换。

Incident_Persons_List.Rows(CInt(row)) 

通过

row 

,或者使用基本的结构,而不是的foreach。 Somethink像

For row = 0 To Incident_Persons_List.Rows.Count - 1 Step 1 
    //SomeStuff 
Next 
相关问题