2009-09-10 8 views
6

행 번호 DataGridView 셀은 어떻게 가져 옵니까? 특히 사용자가 단일 셀을 선택한 경우 어떻게 행 번호를 가져올 수 있습니까? 사용자가 선택한 것을 기반으로 특정 셀에 액세스해야합니다.DataGridView에서 행 번호 가져 오기

RemoveAt 메서드를 사용하여 포커스를 제거 할 수 있지만 포커스가있는 행 번호를 가져올 수 없다는 것을 알고 있습니다.

도움 주셔서 감사합니다. 당신은 단지에 RowIndex을 사용할 수 있습니다

답변

17

current cell : 당신은 또한이 솔루션을 사용할 수 있습니다

var row = dataGridView1.CurrentCell.RowIndex; 
0

그것은 거의 동일하지만 :

var row = dataGridView1.CurrentRow.Index 
0

또 다른 방법은 당신이와 사용자의 상호 작용을 추적해야하는 경우 a 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 
관련 문제