none
Validación columna DataGridView RRS feed

  • Pregunta

  • Hola,

    Necesito hacer mi propia clase columna para un datagridview que valide datos decimales y que dicha columna sea también de tipo decimal, ¿alguna idea?.

    Gracias.

    • Cambiado Enrique M. Montejo miércoles, 8 de enero de 2014 8:51 Controles de Windows Forms
    viernes, 3 de enero de 2014 13:15

Todas las respuestas

  • Saludos:

    Trata esto:

    Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
        ByVal e As DataGridViewCellValidatingEventArgs) _
        Handles dataGridView1.CellValidating
    
        Dim headerText As String = _
            dataGridView1.Columns(e.ColumnIndex).HeaderText
    
        ' Abort validation if cell is not in the CompanyName column.
        If Not headerText.Equals("CompanyName") Then Return
    
        ' Confirm that the cell is not empty.
        If (String.IsNullOrEmpty(e.FormattedValue.ToString())) Then
            dataGridView1.Rows(e.RowIndex).ErrorText = _
                "Company Name must not be empty"
            e.Cancel = True
        End If
    End Sub
    
    Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
        Handles dataGridView1.CellEndEdit
    
        ' Clear the row error in case the user presses ESC.   
        dataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
    
    End Sub
    

    Mas de esto lo encuentras en http://msdn.microsoft.com/es-es/library/ykdxa0bc(v=vs.110).aspx

    Espero ayude.

    JOSE LUIS

    viernes, 3 de enero de 2014 19:54
  • Hola,

    Gracias por responder pero estaba buscando algo como esto...

    Imports System.ComponentModel Imports misclasesdeayuda Public Class editorvalorceldatextbox Inherits TextBox Implements IDataGridViewEditingControl Private WithEvents datagrid As DataGridView Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView Get Return datagrid End Get Set(ByVal value As System.Windows.Forms.DataGridView) datagrid = value End Set End Property Public Property EditingControlFormattedValue() As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue Get Return Me.Text End Get Set(ByVal value As Object) Me.Text = value End Set End Property Private númerofila As Integer Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex Get Return númerofila End Get Set(ByVal value As Integer) númerofila = value End Set End Property Private valorcambiado As Boolean = False Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged Get Return valorcambiado End Get Set(ByVal value As Boolean) valorcambiado = value End Set End Property Public ReadOnly Property EditingPanelCursor() As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor Get Return MyBase.Cursor End Get End Property Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange Get Return False End Get End Property Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle) Implements System.Windows.Forms.IDataGridViewEditingControl.ApplyCellStyleToEditingControl Me.Font = dataGridViewCellStyle.Font Me.ForeColor = dataGridViewCellStyle.ForeColor Me.BackColor = dataGridViewCellStyle.BackColor End Sub Public Function EditingControlWantsInputKey(ByVal keyData As System.Windows.Forms.Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlWantsInputKey Select Case keyData And Keys.KeyCode Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp Return True Case Else Return False End Select End Function Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue Return Me.Text End Function Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit 'No es necesario codificar este método End Sub Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) valorcambiado = True Me.EditingControlDataGridView.NotifyCurrentCellDirty(True) MyBase.OnTextChanged(e) End Sub Private Sub Midatagridview1_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles datagrid.DataError Me.Text = Nothing MsgBox("El dato ingresado es incorrecto", MsgBoxStyle.Information, "Atención") End Sub End Class Public Class celdatextbox Inherits DataGridViewTextBoxCell Public Overrides ReadOnly Property EditType() As System.Type Get Return GetType(editorvalorceldatextbox) End Get End Property Public Overrides ReadOnly Property ValueType() As System.Type Get Return GetType(Decimal) End Get End Property Public Overrides ReadOnly Property DefaultNewRowValue() As Object Get Return Nothing End Get End Property Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle) MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle) Dim control As editorvalorceldatextbox = CType(DataGridView.EditingControl, editorvalorceldatextbox) control.Text = initialFormattedValue End Sub End Class Public Class midatagridnumericcolumn Inherits DataGridViewColumn Private _tipodedato As tiposdedatos = tiposdedatos.Enteros Public Sub New() MyBase.New(New celdatextbox()) End Sub Public Enum tiposdedatos Enteros Decimales End Enum Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell Get Return MyBase.CellTemplate End Get Set(ByVal value As System.Windows.Forms.DataGridViewCell) MyBase.CellTemplate = value End Set End Property <DisplayName("Tipo de Dato")> _ Public Property tipodedato() As tiposdedatos Get Return _tipodedato End Get Set(ByVal value As tiposdedatos) _tipodedato = value End Set End Property Public Overrides Function Clone() As Object Dim columna As midatagridnumericcolumn = DirectCast(MyBase.Clone(), midatagridnumericcolumn) columna.tipodedato = Me.tipodedato Return columna End Function End Class

    Este código en su mayor parte lo he sacado del msdn donde explican cómo hacer una columna de tipo DateTimePicker solo que personalizado para lo que quiero.

    Como podeis comprobar tengo que controlar la excepción con el evento Midatagridview1_DataError, no veo otra forma de hacerlo. Espero que así se entienda mejor lo que pretendo y a ver si me podeis ayudar porque seguro que ésta no es la manera correcta de hacerlo.

    Gracias.


    • Editado lytos viernes, 3 de enero de 2014 21:31
    viernes, 3 de enero de 2014 21:25
  • ¿Algún consejo?...

    • Editado lytos domingo, 5 de enero de 2014 14:15
    domingo, 5 de enero de 2014 14:15