DataGridViewで行番号を取得する
-
05-07-2019 - |
質問
行番号DataGridViewセルを取得するにはどうすればよいですか?具体的には、ユーザーが単一のセルを選択した場合、どのようにしてその行番号を取得できますか?ユーザーが選択した内容に基づいて特定のセルにアクセスする必要があります。
RemoveAtメソッドを使用してフォーカスで削除できることは知っていますが、明らかにフォーカスで行番号を取得できませんか?
助けてくれてありがとう!
他のヒント
これは正常に動作します。
Private Sub DataGridView1_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If e.RowIndex >= 0 Then
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
End If
End Sub
ほぼ同じですが、このソリューションを使用することもできます:
var row = dataGridView1.CurrentRow.Index
DataGridViewとのユーザーインタラクションを追跡する必要がある場合の別の方法: 私の場合、Me.I_SelColとMe.I_SelRowの列と行の座標を使用するいくつかの汎用関数に追加の処理がありますが、OPには関係ないため、それを示していません。
よろしく、 ロブ
Private Sub I_DataGridView_CurrentCellChanged(sender As Object, e As EventArgs) Handles I_DataGridView.CurrentCellChanged
If Me.I_DataGridView.CurrentCellAddress.X < 0 Or Me.I_DataGridView.CurrentCellAddress.Y < 0 Then Exit Sub
' The Windows Me.I_DataGridView object will have already deselected the current cell and selected the
' new cell as per user navigation using mouse or cursor keys. We just need to store the current
' co-ordinates for the currently selected cell.
Me.I_SelCol = Me.I_DataGridView.CurrentCellAddress.X
Me.I_SelRow = Me.I_DataGridView.CurrentCellAddress.Y
Exit Sub
所属していません StackOverflow