2009-09-10 79 views
6

如何获取行号DataGridView单元格?具体而言,如果用户选择了单个单元格,那么如何获得该行号?它需要根据用户选择的内容来访问特定的单元格。在DataGridView中获取行号

我知道RemoveAt方法可以用于在Focus处移除,但是显然不能获得焦点处的行号?

感谢您的帮助!

回答

0

它几乎是相同的,但你也可以使用此解决方案:

var row = dataGridView1.CurrentRow.Index 
0

另一种方式,如果你需要跟踪用户交互一个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 
1

这个工作正常。

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