如何獲取行號DataGridView單元格?具體而言,如果用戶選擇了單個單元格,那麼如何獲得該行號?它需要根據用戶選擇的內容來訪問特定的單元格。在DataGridView中獲取行號
我知道RemoveAt方法可以用於在Focus處移除,但是顯然不能獲得焦點處的行號?
感謝您的幫助!
如何獲取行號DataGridView單元格?具體而言,如果用戶選擇了單個單元格,那麼如何獲得該行號?它需要根據用戶選擇的內容來訪問特定的單元格。在DataGridView中獲取行號
我知道RemoveAt方法可以用於在Focus處移除,但是顯然不能獲得焦點處的行號?
感謝您的幫助!
您可以簡單地使用RowIndex的current cell:
var row = dataGridView1.CurrentCell.RowIndex;
它幾乎是相同的,但你也可以使用此解決方案:
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
這個工作正常。
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