2009-09-10 47 views
6

Làm thế nào để bạn nhận được số dòng DataGridView ô? Cụ thể, nếu người dùng đã chọn một ô đơn lẻ, bạn có thể lấy số hàng đó bằng cách nào? Nó cần truy cập vào một ô cụ thể dựa trên những gì người dùng đã chọn.Bắt số hàng trong DataGridView

Tôi biết rằng phương pháp RemoveAt có thể được sử dụng để xóa tại Focus, nhưng bạn không thể lấy được số dòng ở tiêu điểm rõ ràng?

Cảm ơn sự giúp đỡ!

Trả lời

17

Bạn chỉ có thể sử dụng RowIndex trên current cell:

var row = dataGridView1.CurrentCell.RowIndex; 
0

Nó gần như là giống nhau nhưng bạn cũng có thể sử dụng giải pháp này:

var row = dataGridView1.CurrentRow.Index 
0

Một cách khác nếu bạn cần phải theo dõi người dùng tương tác với một DataGridView: Trong trường hợp của tôi có thêm xử lý trong một số chức năng chung mà sử dụng Me.I_SelCol và Me.I_SelRow cột và hàng tọa độ, nhưng tôi đã không cho thấy rằng bởi vì nó không liên quan đến OP.

Trân trọng, Rob

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

này hoạt động tốt.

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 
Các vấn đề liên quan