none
Dar formato a una columna de datagridview RRS feed

  • Pregunta

  • Buenas estoy intentando darle formato a una columna de un datagridview este fotmato (__/__/__) son numeros no fecha y lo que quiero conseguir es intruzco los 2 primeros pues que salte al otro hueco solo puedo introducir 3 pares de 2 numeros 

    he usado esto pero no me sale la columna con el formato

    Dgv_Temperaturas.Columns(1).DefaultCellStyle.Format = "##/##/##00/00/00"

    Gracias 

    lunes, 27 de abril de 2020 16:44

Todas las respuestas

  • Haber si esto te sirve ANANPO_49

    Public Class Form1

        Private TeclaPresionada As String
        Private UltimoChar As String
        Private row(4) As String
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 1
            DataGridView1.Columns(0).Name = "Referencia"
        End Sub
        Private Sub BtnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAgregar.Click
            row(0) = TbDatos.Text
            Dim rowToSave As String() = New String() {row(1), row(2), row(3), row(4)}
            DataGridView1.Rows.Add(row)
            TbDatos.Text = ""
        End Sub
        Private Sub TbDatos_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles TbDatos.KeyPress
            Numeros(e, sender)
        End Sub
        Private Sub TbDatos_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TbDatos.TextChanged
            MaskDatos(sender)
        End Sub
        Public Sub Numeros(ByVal Tecla As KeyPressEventArgs, ByVal Caja As System.Object)
            TeclaPresionada = Tecla.KeyChar
            If Caja.SelectionStart = 1 Then
                UltimoChar = Caja.text.chars(Caja.SelectionStart - 1)
            Else
                UltimoChar = ""
            End If
            If (Asc%(Tecla.KeyChar) < 48 Or Asc%(Tecla.KeyChar) > 57) And (Asc%(Tecla.KeyChar) <> 8) Then
                Tecla.KeyChar = ""
            End If
        End Sub
        Public Sub MaskDatos(ByVal Caja As Object)
            Dim Cursor As Integer
            Dim Mask As String
            Caja.MaxLength = 10
            If Asc%(TeclaPresionada) <> 8 Then
                Cursor = Caja.SelectionStart
                If Len(Caja.text) = 2 Or Len(Caja.text) = 5 Then
                    Cursor = Cursor + 1
                End If
                Mask = Replace(Caja.text, "/", "")
                If Len(Mask) < 5 And Len(Mask) >= 2 Then
                    Mask = Mask.Insert(2, "/")
                End If
                If Len(Mask) >= 5 Then
                    Mask = Replace(Caja.text, "/", "")
                    Mask = Mask.Insert(2, "/")
                    Mask = Mask.Insert(5, "/")
                End If
                Caja.text = Mask
                Caja.SelectionStart = Cursor
            Else
                If UltimoChar = "/" Then
                    SendKeys.Send("(BACKSPACE)")
                End If
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Close()
        End Sub
    End Class

    jueves, 7 de mayo de 2020 15:52
  • Hola:

    En un Form con 1 DGV, copia y pega el siguiente codigo

    Option Strict On
    Option Explicit On
    Public Class Form3
        Private mdtDataTable As New DataTable
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
            mdtDataTable.Columns.Add(New DataColumn("Id", Type.GetType("System.String")))
            DataGridView1.DataSource = mdtDataTable
            ' quitar la columna original que sustituiremos por nuestra columna personalizada
            Me.DataGridView1.Columns.Remove("Id")
            ' crear un objeto de nuestra columna personalizada y añadirlo a la colección de columnas del DataGridView
            Dim oMCCol As MaskColumna = New MaskColumna()
            oMCCol.Width = 8
            oMCCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            oMCCol.Mascara = "00/00/00"
            oMCCol.HeaderText = "Numero"
            oMCCol.DataPropertyName = "Id"
            Me.DataGridView1.Columns.Add(oMCCol)
        End Sub
    End Class

    Tienes que añadir esta 3 clases


    Public Class MaskColumna
        Inherits DataGridViewColumn
        Private msMascara As String
        ' propiedades
        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
        Public Property Mascara() As String
            Get
                Return msMascara
            End Get
            Set(ByVal value As String)
                msMascara = value
            End Set
        End Property
        'métodos
        Public Sub New()
            MyBase.New(New MaskCelda())
        End Sub
    End Class

    Public Class MaskCelda
        Inherits DataGridViewTextBoxCell

        ' propiedades
        Public Overrides ReadOnly Property EditType() As System.Type
            Get
                Return GetType(MaskValorCelda)
            End Get
        End Property
        Public Overrides ReadOnly Property ValueType() As System.Type
            Get
                Return GetType(Date)
            End Get
        End Property
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                Return Date.Now
            End Get
        End Property
        ' métodos
        Public Sub New()
            '....
        End Sub
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctlEditorValorCelda As MaskValorCelda = CType(DataGridView.EditingControl, MaskValorCelda)
            Dim colColumnaPersonalizada As MaskColumna = CType(Me.OwningColumn, MaskColumna)
            ctlEditorValorCelda.Mask = colColumnaPersonalizada.Mascara
            ctlEditorValorCelda.Text = CStr(initialFormattedValue)
        End Sub
    End Class

    Public Class MaskValorCelda
        Inherits MaskedTextBox
        Implements IDataGridViewEditingControl
        ' variables de uso interno
        Private nNumeroFila As Integer
        Private oDGV As DataGridView
        Private bValorCambiado As Boolean = False
        ' propiedades
        Public Property EditingControlDataGridView() As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return oDGV
            End Get
            Set(ByVal value As System.Windows.Forms.DataGridView)
                oDGV = 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 = CStr(value)
            End Set
        End Property
        Public Property EditingControlRowIndex() As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return nNumeroFila
            End Get
            Set(ByVal value As Integer)
                nNumeroFila = value
            End Set
        End Property
        Public Property EditingControlValueChanged() As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return bValorCambiado
            End Get
            Set(ByVal value As Boolean)
                bValorCambiado = 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
        ' métodos
        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
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            bValorCambiado = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(e)
        End Sub
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
            ' no es necesario codificar este método
        End Sub
    End Class

    Un saludo desde Bilbo
    Carlos
    jueves, 7 de mayo de 2020 19:57