Pregunta

¿Cómo se obtiene el número de fila de la celda DataGridView? Específicamente, si un usuario ha seleccionado una sola celda, ¿cómo puede obtener ese número de fila? Necesita acceder a una celda en particular según lo que el usuario haya seleccionado.

Sé que el método RemoveAt se puede usar para eliminar en el foco, pero aparentemente no puede obtener el número de fila en el foco.

¡Gracias por la ayuda!

¿Fue útil?

Solución

Simplemente puede usar RowIndex en la celda actual : / p>

var row = dataGridView1.CurrentCell.RowIndex;

Otros consejos

este funciona bien.

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

Es casi lo mismo, pero también puede usar esta solución:

var row = dataGridView1.CurrentRow.Index

Otra forma si necesita rastrear la interacción del usuario con un DataGridView: En mi caso, hay un procesamiento adicional en algunas funciones genéricas que usan las coordenadas de columna y fila Me.I_SelCol y Me.I_SelRow, pero no lo he demostrado porque no es relevante para el OP.

Saludos cordiales, 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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top