• Lunes 29 de Abril de 2024, 06:18

Autor Tema:  Eliminar La última Fila de un DataGridView  (Leído 10399 veces)

DriverSoftVzla

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Eliminar La última Fila de un DataGridView
« en: Domingo 19 de Julio de 2009, 05:58 »
0
HOLA AMIGOS.

LES HECHO EL CUENTO:
TENGO UN DATAGRIDVIEW DESCONECTADO, LO QUE QUIERO ES ELIMINAR LA ULTIMA FILA CUANDO ESTA VACIA, PERO QUIERO QUE LO HAGA AUTOMATICAMENTE. ME EXPLICO, SI ESTOY EN LA ULTIMA FILA Y ME MUEVO A LA FILA ANTERIOR, QUIERO QUE LA ELIMINE AUTOMATICAMENTE.

SI LO HAGO MANUALMENTE FUNCIONA, EJEMPLO:

     Case Keys.F4     'Elimina Una Fila del DatagridView
     If (_DGV1.RowCount > 0) Then
           Me._DGV1.Rows.Remove(Me._DGV1.CurrentRow)
     End If

HE PROBADO ELIMINARLA EN LOS EVENTOS: RowValidating, RowValidated, RowLeave
EJEMPLO:

Private Sub _DGV1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles _DGV1.RowValidated
If (_DGV1.RowCount > 0) Then
If _DGV1.Rows(e.RowIndex).Cells(0).Value() = String.Empty Then
Me._DGV1.Rows.Remove(Me._DGV1.CurrentRow)
End If
End If
End Sub

Y ARROJA: "La operación no se puede realizar en este controlador de eventos"

NO SE DONDE HACERLO, GRACIAS POR TODA LA AYUDA PRESTADA.

fm89-10

  • Miembro activo
  • **
  • Mensajes: 75
  • Nacionalidad: ni
    • Ver Perfil
Re: Eliminar La última Fila de un DataGridView
« Respuesta #1 en: Miércoles 22 de Julio de 2009, 21:43 »
0
hola para empezar no escribas en mayusculas.Mira lo que puedes hacer es crear un evento de teclado que cuando presiones la flecha de arriba del teclado  te elimine la fila.(si el cursor esta en la fila vacia y presionas flecha arriba te la elimine).

Código: vb.net
  1.  
  2.   Private Sub DataGridView1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
  3.         Dim v As Integer
  4.         v = e.KeyValue
  5.         If (v = 38) Then
  6.         If (_DGV1.RowCount > 0) Then
  7.        DataGridView1.AllowUserToAddRows = False
  8.      End If
  9.       
  10.       End If
  11.  
  12.     End Sub
  13.  
  14.  


alguna duda me dices :hola:

DriverSoftVzla

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Re: Eliminar La última Fila de un DataGridView
« Respuesta #2 en: Jueves 23 de Julio de 2009, 07:36 »
0
Hola, gracias por responder.

Permiteme escribir algo en mayuscula,  MUCHAS GRACIAS

y ahora si, nada en mayus.

Tomando tu ejemplo se me prendio el bombillo, y lo hice asi, claro aun estoy averiguando como controlo cuando cambie de fila con el mouse, pero mientras tenemos este adelanto:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData as
  System.Windows.Forms.Keys) As Boolean

         If (Not _DGV1.Focused) AndAlso _
            (Not _DGV1.IsCurrentCellInEditMode) Then Return MyBase.ProcessCmdKey(msg, keyData)

        Select Case keyData

              Case Keys.Up, Keys.Down, Keys.         'Eliminar Fila Vacia
                 If Eliminar_Fila_Vacia() Then
                     Return True
                  End If

              Case Keys.F8
                 If Me._DGV1.Rows(Me._DGV1.CurrentCell.RowIndex).Cells(0).Selected Then         'Cells(0) es la columna CODIGO
                    SeleccionarCodigo()
                 End If

       End Select

       Return MyBase.ProcessCmdKey(msg, keyData)

 End Function

 
 Private Function Eliminar_Fila_Vacia() As Boolean
        Dim i As Integer = _DGV1.CurrentCell.RowIndex               'Indice Fila a Eliminar
        If (_DGV1.RowCount > 0) Then
            If _DGV1.Rows(i).Cells(0).Value() = String.Empty Then
                _DGV1.Rows.Remove(Me._DGV1.CurrentRow)              'Elimina Fila
                _DGV1.CurrentCell = _DGV1.Rows(i - 1).Cells(0)      'Selecciono Celda Anterior
                Return True
            End If
        End If
        Return False
    End Function