IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

DataGridView, correspondance Colonne créée (DataGridViewDateTime) - Colonne DataTable


Sujet :

VB.NET

  1. #1
    Membre actif

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    291
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 291
    Points : 270
    Points
    270
    Par défaut DataGridView, correspondance Colonne créée (DataGridViewDateTime) - Colonne DataTable
    Salut tout le monde,

    J'ai trouvé un code, chez Microsoft, qui permet d'avoir dans un DataGridView une colonne de type DateTime (Affichage du calendrier pour la sélection d'une date).

    Voilà pour tout ceux qui cherchent (ou cherchaient )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    Imports System
    Imports System.Windows.Forms
     
    Public Class CalendarColumn
        Inherits DataGridViewColumn
     
        Public Sub New()
            MyBase.New(New CalendarCell())
        End Sub
     
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
     
                ' Ensure that the cell used for the template is a CalendarCell.
                If (value IsNot Nothing) AndAlso _
                    Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) _
                    Then
                    Throw New InvalidCastException("Must be a CalendarCell")
                End If
                MyBase.CellTemplate = value
     
            End Set
        End Property
     
    End Class
     
    Public Class CalendarCell
        Inherits DataGridViewTextBoxCell
     
        Public Sub New()
            ' Use the short date format.
            Me.Style.Format = "d"
        End Sub
     
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
     
            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)
     
            Dim ctl As CalendarEditingControl = _
                CType(DataGridView.EditingControl, CalendarEditingControl)
            ctl.Value = CType(Me.Value, DateTime)
     
        End Sub
     
        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(CalendarEditingControl)
            End Get
        End Property
     
        Public Overrides ReadOnly Property ValueType() As Type
            Get
                ' Return the type of the value that CalendarCell contains.
                Return GetType(DateTime)
            End Get
        End Property
     
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                ' Use the current date and time as the default value.
                Return DateTime.Now
            End Get
        End Property
     
    End Class
     
    Class CalendarEditingControl
        Inherits DateTimePicker
        Implements IDataGridViewEditingControl
     
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
     
        Public Sub New()
            Me.Format = DateTimePickerFormat.Short
        End Sub
     
        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
     
            Get
                Return Me.Value.ToShortDateString()
            End Get
     
            Set(ByVal value As Object)
                If TypeOf value Is String Then
                    Me.Value = DateTime.Parse(CStr(value))
                End If
            End Set
     
        End Property
     
        Public Function GetEditingControlFormattedValue(ByVal context _
            As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
     
            Return Me.Value.ToShortDateString()
     
        End Function
     
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
            DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
     
            Me.Font = dataGridViewCellStyle.Font
            Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
            Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
     
        End Sub
     
        Public Property EditingControlRowIndex() As Integer _
            Implements IDataGridViewEditingControl.EditingControlRowIndex
     
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
     
        End Property
     
        Public Function EditingControlWantsInputKey(ByVal key As Keys, _
            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
            Implements IDataGridViewEditingControl.EditingControlWantsInputKey
     
            ' Let the DateTimePicker handle the keys listed.
            Select Case key 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 Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
            Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
     
            ' No preparation needs to be done.
     
        End Sub
     
        Public ReadOnly Property RepositionEditingControlOnValueChange() _
            As Boolean Implements _
            IDataGridViewEditingControl.RepositionEditingControlOnValueChange
     
            Get
                Return False
            End Get
     
        End Property
     
        Public Property EditingControlDataGridView() As DataGridView _
            Implements IDataGridViewEditingControl.EditingControlDataGridView
     
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set
     
        End Property
     
        Public Property EditingControlValueChanged() As Boolean _
            Implements IDataGridViewEditingControl.EditingControlValueChanged
     
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
     
        End Property
     
        Public ReadOnly Property EditingControlCursor() As Cursor _
            Implements IDataGridViewEditingControl.EditingPanelCursor
     
            Get
                Return MyBase.Cursor
            End Get
     
        End Property
     
        Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
     
            ' Notify the DataGridView that the contents of the cell have changed.
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnValueChanged(eventargs)
     
        End Sub
     
    End Class
    L'exemple qui va avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
            Dim col As New CalendarColumn()
            Me.DataGridView1.Columns.Add(col)
            col.Name = "Date"
     
            Me.DataGridView1.RowCount = 5
            Dim row As DataGridViewRow
     
            For Each row In Me.DataGridView1.Rows
            row.Cells(0).Value = DateTime.Now
            Next row
    Mais mon problème est que je charge une table dans ce DataGridView et je voudrais que cette colonne DataColumnDateTime corresponde avec la colonne (Date) de cette table.

    Auriez-vous des pistes ?

    Merci d'avance

    Gdal

  2. #2
    Membre chevronné
    Avatar de olsimare
    Inscrit en
    Décembre 2006
    Messages
    1 179
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 179
    Points : 1 776
    Points
    1 776
    Par défaut
    Bonjour.

    Personnellement je positionne le .AutoGenerateColumns du DGV à False et je génére moi-même les colonnes d'un type donné suivant paramétrage.

    Dans ton cas, tu pourrais généré tes colonnes et prendre le type CalendarColumn si le champ de la base est de type date.

    Cdt.
    Bon à savoir : la touche F1 ne sert pas à commander des places pour le grand prix de Belgique.

  3. #3
    Membre actif

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    291
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 291
    Points : 270
    Points
    270
    Par défaut
    Merci, mais aurais-tu un petit exemple

    Gdal

  4. #4
    Membre chevronné
    Avatar de olsimare
    Inscrit en
    Décembre 2006
    Messages
    1 179
    Détails du profil
    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 179
    Points : 1 776
    Points
    1 776
    Par défaut
    Re

    En voilà un spécial pour toi :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    Public Class Form7
     
        Private Sub Form7_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            Dim strSqlquery As String = _
            "SELECT * FROM TableTestGDAL"
            Dim ad As New OleDb.OleDbDataAdapter(New OleDb.OleDbCommand(strSqlquery, CLFWAddMy.CurrentConnection))
            Dim ds As New DataSet
            ad.Fill(ds)
     
            Me.DataGridView1.AutoGenerateColumns = False
     
            For Each c As DataColumn In ds.Tables(0).Columns
                Select Case c.DataType.Name
                    Case "DateTime"
                        Dim dgvC As New CLFWAdd.CalendarColumn
                        Dim i As Integer
                        i = Me.DataGridView1.Columns.Add(dgvC)
                        Me.DataGridView1.Columns(i).DataPropertyName = c.ColumnName
                    Case "Boolean"
                        Dim dgvC As New DataGridViewCheckBoxColumn
                        Dim i As Integer
                        i = Me.DataGridView1.Columns.Add(dgvC)
                        Me.DataGridView1.Columns(i).DataPropertyName = c.ColumnName
                    Case Else
                        Dim dgvC As New DataGridViewTextBoxColumn
                        Dim i As Integer
                        i = Me.DataGridView1.Columns.Add(dgvC)
                        Me.DataGridView1.Columns(i).DataPropertyName = c.ColumnName
                End Select
            Next
     
            Me.DataGridView1.DataSource = ds.Tables(0)
     
        End Sub
     
    End Class
    Nota : CLFWAddMy.CurrentConnection = ma connection à remplacer par la tienne.
    CLFWAdd.CalendarColumn = un colonne de type calendar (même chose que la tienne donc tu remplaces)
    La base est une base access avec une table "TableTestGDAL" qui contient une colonne de type date, une de type string et une de type boolean.

    Voilà.

    Cdt.
    Bon à savoir : la touche F1 ne sert pas à commander des places pour le grand prix de Belgique.

  5. #5
    Membre actif

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    291
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 291
    Points : 270
    Points
    270
    Par défaut
    Salut,

    T'es un chef !

    Merci beaucoup.

    J'ai trouvé quelque chose d'identique dans l'aide
    DataGridViewColumn.DataPropertyName, propriété
    ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.fr/cpref17/
    html/P_System_Windows_Forms_DataGridViewColumn_DataPropertyName.htm

    (Attention il s'agit du lien à insérer dans l'aide)

    J'ai modifié la classe Public Class CalendarCell
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
            Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
     
            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)
     
            Dim ctl As CalendarEditingControl = _
                CType(DataGridView.EditingControl, CalendarEditingControl)
     
            If Me.Value.ToString = "" Then
                Me.Value = CType(Now, DateTime)
            Else
                ctl.Value = CType(Me.Value, DateTime)
            End If
     
        End Sub
    J'ai ajouté un test null autrement cela plante !

    Encore merci.

    Gdal

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 03/10/2008, 16h06
  2. [VB2005] DatagridView et colonnes crées manuellement.
    Par azerty53 dans le forum VB.NET
    Réponses: 2
    Dernier message: 07/06/2007, 21h36
  3. Réponses: 3
    Dernier message: 19/07/2006, 14h28
  4. [VB.NET] définir format colonne datetime d'un datatable
    Par HULK dans le forum Windows Forms
    Réponses: 2
    Dernier message: 06/12/2005, 16h58
  5. [Excel] Affichage de la réf correspondant à un N° de colonne
    Par Julien81 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 23/02/2005, 11h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo