2016-06-28 45 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 
相關問題