2016-06-28 397 views
1

VB.Net(Winforms)で次のコードを使用して、DataGridViewをループし、不要な行を非表示にしています。呼び出しのターゲットによってVB.Net例外がスローされました

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

私はこれがある理由を理解するために私は考えることができるすべてのものをしようとしてきた:デバッガはIF文の最初の行になると、私は次のエラーを取得する

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 

。私は間違いを見てきましたが、この例外がスローされたときには誰もが全く異なる問題を抱えているようです。

私はどのように比較をしているのですか?

UPDATE 1

  1. Iは実際の例外がスローされることを明らかにしたように、私はTry/CatchCint命令
  2. を除去したFor Eachを除去しFor i = 0 to Incident_Persons_list.Rows.Count
  3. に置き換えたある:

Row associated with the currency manager's position cannot be made invisible.

すべてが今、以下のコードで正常に動作している2

UPDATE:助けを

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

**完全な例外の詳細を表示すると、InnerExceptionは何がうまくいかないかを知る上で重要です。何の内部例外はありません –

+0

が、それは私が – SilverShotBee

+0

ラップのtry/catch構造のコードブロックを直面してる問題だし、InnerExcpetion –

答えて

2

TargetInvocationExceptionようSomethink:(その例外は本当に参考になっていないため)何が起こっているのかを知る方法

The exception that is thrown by methods invoked through reflection

?あなたはTry/Catch構造と呼び出し元のブロックをsurroung、その後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

アドバイスとして、 UIアクションに関連付けられたイベントを処理します。ユーザーアクションはアプリケーションで最も予測不可能なものであり、予期しない動作の候補になりました。また、InnerExceptionはTargetInvocationExceptionに固有ではないことに注意してください。 "最初のレベル"について十分な情報が見つからないときは、常にこのプロパティを見てください。 –

2

あなたの行変数は、Incident_Persons_List.Rowsの列挙された要素であり、コレクション内の要素のインデックスではないため、置き換えてください。

row 

または構造の代わりに、foreachのための基本的なを使用することにより

Incident_Persons_List.Rows(CInt(row)) 

For row = 0 To Incident_Persons_List.Rows.Count - 1 Step 1 
    //SomeStuff 
Next 
関連する問題