La edición quedará condicionada a los tipos de objetos que hayamos añadido al DataGrid.
Podemos editar solo cuando hay un textbox.
Private Sub DataGrid_CellValidating(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) _
Handles DataGrid.CellValidating
If DataGrid.Rows(e.RowIndex).IsNewRow Then Return
Select Case e.ColumnIndex
Case 0
If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
MsgBox("No puede quedar en blanco.", MsgBoxStyle.Information, NomProgram)
e.Cancel = True
Else
If e.FormattedValue.ToString().Length > 3 Then
MsgBox("El código excede de la longitud.", MsgBoxStyle.Information, NomProgram)
e.Cancel = True
End If
End If
Case 1
If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
MsgBox("No puede quedar en blanco.", MsgBoxStyle.Information, NomProgram)
e.Cancel = True
End If
End Select
End Sub
Private Sub DataGrid_CellValidated(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGrid.CellValidated
Dim Valor As String
Select Case e.ColumnIndex
Case 0
Valor = DataGrid.CurrentCell.Value.ToString
If Valor <> "" Then
Valor = Format(CInt(Valor), "000")
End If
DataGrid.CurrentCell.Value = Valor
Case 2
Valor = DataGrid.CurrentCell.Value.ToString
If Valor <> "" Then
Valor = Format(CInt(Valor), "##0.00")
End If
DataGrid.CurrentCell.Value = Valor
End Select
End Sub
También podemos optar por esta otra solución.
Private Sub Datagrid_EditingControlShowing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
Handles Datagrid.EditingControlShowing
' Evitamos la columna del combo
Select Case DataGrid.CurrentCell.ColumnIndex
Case 0
Case Else
Dim CeldasGrid As TextBox = CType(e.Control, TextBox)
' Agregar el controlador de eventos para el KeyPress
AddHandler CeldasGrid.KeyPress, AddressOf Validar_Celdas
AddHandler CeldasGrid.Validating, AddressOf Validando
End Select
End Sub
El primer paso es en este evento ejecutar el código que vemos.
De esa forma conseguimos
Dim CeldasGrid As TextBox = CType(e.Control, TextBox)
Crear un objeto del tipo TextBox, o lo que es lo mismo nos lo llevamos al terreno que nos interesa.
De esa forma podemos usar los siguientes procedimientos, que ya tendremos escritos.
Private Sub Validando(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs)
Validacion(2, CType(sender, TextBox), e.Cancel)
End Sub
Private Sub Validar_Celdas(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs)
' Capturamos el índice de la columna
Dim Columna As Integer = DataGrid.CurrentCell.ColumnIndex
TratarTeclado(Columna, e)
End Sub
De tal forma que lo que podemos es manejar la celda del DataGrid, como un textbox más.
' Agregar el controlador de eventos para el KeyPress
AddHandler CeldasGrid.KeyPress, AddressOf Validar_Celdas
AddHandler CeldasGrid.Validating, AddressOf Validando
Ya que aquí asociamos el evento keypress normal que usamos siempre para el control del teclado.
AddHandler CeldasGrid.KeyPress, AddressOf Validar_Celdas
Private Sub Validar_Celdas
Y en este usamos creamos el evento validating para la llamada al procedimiento de validación que usemos siempre.
AddHandler CeldasGrid.Validating, AddressOf Validando
Private Sub Validando
Síguenos en: Facebook Sobre aulaClic Política de Cookies