0

私のDataGridViewのSelectionChangedイベントのコードは次のとおりです。

 Dim a as Integer
 a = DataGridView1.CurrentRow.Index 'this certain line contains the error
 TextBox1.Text = DataGridView1.Item(0,a).Value
 'and all that jazz

コードは実行されますが、セルをクリックしてから列ヘッダーをクリックすると、エラーが表示されます。

このコード ブロックを CellContentClick という別のイベントに配置しようとしましたが、datagridview 内のセルをクリックしても、SelectionChanged に比べて応答が遅くなります。

これを修正する方法についてのアイデアはありますか?

4

2 に答える 2

0

OK - これが古い投稿であることはわかっていますが、上記を読んでこのコードを思いついたので、共有したいと思いました。

  Private Function TryReturnString(ByRef obj As Object) As String
    Dim rtn As String = String.Empty
    If obj IsNot Nothing Then
      rtn = obj.ToString
    End If
    Return rtn
  End Function

または上記の場合:

  Private Function TryReturnIndex(ByRef curRow As DataGridViewRow) As Integer
    Dim rtn As Integer = 0
    If curRow IsNot Nothing Then
      rtn = curRow.Index
    End If
    Return rtn
  End Function
于 2016-09-21T18:33:36.237 に答える