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 :

DataGridViewColumn et EditingControl personalisée


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2012
    Messages
    337
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Vaucluse (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2012
    Messages : 337
    Par défaut DataGridViewColumn et EditingControl personalisée
    Bonjour,
    Je souhaite créer une colonne de DGV de type RichTextBox. Pour m'entrainer, j'ai repris l'exemple de MSDN sur le DateTimePicker qui fonctionne à merveille.

    Je reste bloqué sur la surcharge de l'initialisation de la cellule.
    Voici le code complet des 3 classes créées :

    La colonne :
    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
     
    Public Class RichTextColumn
        Inherits DataGridViewColumn
     
        Public Sub New()
            MyBase.New(New RichTextCell())
        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(RichTextCell)) _
                    Then
                    Throw New InvalidCastException("Must be a RichTextCell")
                End If
                MyBase.CellTemplate = value
     
            End Set
        End Property
    End Class
    La cellule :
    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
     
    Public Class RichTextCell
        Inherits DataGridViewCell
     
        Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl As RichTextEditingControl = CType(DataGridView.EditingControl, RichTextEditingControl)
            If (Me.Value Is Nothing) Then
                ctl.Rtf = CType(Me.DefaultNewRowValue, String)
            Else
                ctl.Rtf = CType(Me.Value, String)
            End If
        End Sub
     
        Public Overrides ReadOnly Property EditType As System.Type
            Get
                Return GetType(RichTextEditingControl)
            End Get
        End Property
     
        Public Overrides Property ValueType As System.Type
            Get
                Return GetType(String)
            End Get
            Set(value As System.Type)
     
            End Set
        End Property
     
        Public Overrides ReadOnly Property DefaultNewRowValue As Object
            Get
                Return String.Empty
            End Get
        End Property
    End Class
    L'editingControl :
    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
     
    Public Class RichTextEditingControl
        Inherits RichTextBox
        Implements IDataGridViewEditingControl
     
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
     
        Public Sub ApplyCellStyleToEditingControl(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 Property EditingControlDataGridView As System.Windows.Forms.DataGridView Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return dataGridViewControl
            End Get
            Set(value As System.Windows.Forms.DataGridView)
                dataGridViewControl = value
            End Set
        End Property
     
        Public Property EditingControlFormattedValue As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.Rtf
            End Get
            Set(value As Object)
                Me.Rtf = value
            End Set
        End Property
     
        Public Property EditingControlRowIndex As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndexNum
            End Get
            Set(value As Integer)
                rowIndexNum = value
            End Set
        End Property
     
        Public Property EditingControlValueChanged As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueIsChanged
            End Get
            Set(value As Boolean)
                valueIsChanged = value
            End Set
        End Property
     
        Public Function EditingControlWantsInputKey(keyData As System.Windows.Forms.Keys, 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 Not dataGridViewWantsInputKey
            End Select
        End Function
     
        Public ReadOnly Property EditingPanelCursor As System.Windows.Forms.Cursor Implements System.Windows.Forms.IDataGridViewEditingControl.EditingPanelCursor
            Get
                Return MyBase.Cursor
            End Get
        End Property
     
        Public Function GetEditingControlFormattedValue(context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Rtf
        End Function
     
        Public Sub PrepareEditingControlForEdit(selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
     
        End Sub
     
        Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
     
        Protected Overrides Sub OnTextChanged(e As System.EventArgs)
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(e)
        End Sub
    End Class
    Lors de l'exécution, la colonne est bien ajoutée au DGV mais impossible d'y accéder et la cellule ne s'affiche pas comme il le faut.
    Je pense que le problème vient de la classe RichTextCell et plus particulièrement des propriétés .InitializeEditingControl et .ValueType.
    Je ne sais pas quoi mettre d'autre comme type dans l'instruction GetType() ou dans Ctype().

    EDIT: J'ai changé l'héritage de la cellule par DataGridViewTextBoxCell et ça semble fonctionner correctement au niveau affichage.

    Comment afficher un texte au format RTF une fois que l’édition est terminée?

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour rainui

    C'est parce que en fait:
    1/ RichTextCell
    - heritage doit se faire sur DataGridViewTextBoxCell
    - override de InitializeEditingControl:s'il n'y a de contenu rtf(la prop rtf est un string pour rappel ) on se branche en mode texte normal ...
    Ne pas perdre de vue que cette methode est appele à chaque fois qu'on entre dans une cellule dans un dgv et qu'on modifie son contenu...

    - override de la methode clone a ete oublie :elle est charge de cloner la cellule dans tous les rows sinon on ne voit rien...

    2/RichTextEditingControl

    Ne pas perdre de vue :
    -que le control RichTextBox demarre en mode text normal par defaut
    -q'un ensuite il affecte sa prop RichTextBox.rtf (et sa gestion mode rtf) en interne quand on charge un contenu rtf par la methode RichTextBox.LoadFile......

    On ne peut donc le demarrer qu'en mode texte...

    Pour informer RichTextCell quand on est dans sa cellule(enter) initialise en mode normal...que son contenu est modifie on doit disposer d'une methode :
    -permettant de charger un fichier rtf quand on clique (enter) dans
    RichTextCell via son path ...
    Ce path du fichier rtf est recupere via un MenuItem d'un ContextMenu ajoute au RichTextEditingControl et un handler pour opendialog...
    Le path est affecte à un prop FileRTFLoad (rajoute à RichTextEditingControl )
    Des que ce prop change il appelle la methode de base RichTextBox.LoadFile...et initialise la prop rtf.....

    L'ensemble avec d'autes menuitems(font,coleur text ...) est dans ton code revu:

    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
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
     
    Imports System.IO
     
    Public Class RichTextColumn
        Inherits DataGridViewColumn
     
        Public Sub New()
            MyBase.New(New RichTextCell())
        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(RichTextCell)) _
                    Then
                    Throw New InvalidCastException("Must be a RichTextCell")
                End If
                MyBase.CellTemplate = value
     
            End Set
        End Property
    End Class
     
    Public Class RichTextCell
        'Inherits DataGridViewCell
        'MODIF mauvais heritage
        Inherits DataGridViewTextBoxCell
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl As RichTextEditingControl =
                CType(DataGridView.EditingControl, RichTextEditingControl)
            'If (Me.Value Is Nothing) Then
            '    ctl.Rtf = CType(Me.DefaultNewRowValue, String)
            'Else
            '    ctl.Rtf = CType(Me.Value, String)
            'End If
     
     
            'MODIF 
            ' s'il n y a  pas de contenu rtf on se branche en mode texte
            If String.IsNullOrEmpty(ctl.Rtf) And Me.Value Is Nothing Then
                ctl.Text = CType(Me.DefaultNewRowValue, String)
            End If
            ' Sinon on laisse Base RichTextBox se charger en interne
            ' de recuperer le contenu rtf charge apres un appel RichTextBox.Loadfile...
     
        End Sub
        '------------AJOUT: !!!!!! => tres important car elle est charge----
        'de cloner la cellule dans tous les rows(sinon on ne voit rien)
        Public Overrides Function Clone() As Object
     
            Return MyBase.Clone()
        End Function
        Public Overrides ReadOnly Property EditType As System.Type
            Get
                Return GetType(RichTextEditingControl)
            End Get
        End Property
     
        Public Overrides ReadOnly Property ValueType As System.Type
            Get
                Return GetType(String)
            End Get
            ' A SUPPRIMER
            'Set(ByVal value As System.Type)
     
            'End Set
     
        End Property
     
        Public Overrides ReadOnly Property DefaultNewRowValue As Object
            Get
                Return String.Empty
            End Get
        End Property
     
    End Class
     
    Public Class RichTextEditingControl
        Inherits RichTextBox
        Implements IDataGridViewEditingControl
     
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
        ' MENUS CONTEXTUEL POUR GERER RICHTEXTBOX
        ' avec des boites Dialog  
        Private ctx As ContextMenu = New ContextMenu
        Public Sub New()
            ctx.MenuItems.Add("Select Font")
            ctx.MenuItems.Add("Select TextForeColor")
            ctx.MenuItems.Add("Copy Selection")
            ctx.MenuItems.Add("Paste Selection")
            ctx.MenuItems.Add("Load RTF")
            ctx.MenuItems.Add("Save RTF")
            Me.ContextMenu = ctx
            AddHandler ctx.MenuItems(0).Click, AddressOf SelFont
            AddHandler ctx.MenuItems(1).Click, AddressOf SelColor
            AddHandler ctx.MenuItems(2).Click, AddressOf Me.Copy
            AddHandler ctx.MenuItems(3).Click, AddressOf Me.Paste
            AddHandler ctx.MenuItems(4).Click, AddressOf Me.LoadRTF
            AddHandler ctx.MenuItems(5).Click, AddressOf Me.SaveRTF
        End Sub
        'prop fichier charge rtf qui initialisera 
        'la prop base RichTextBox.rtf
        Private m_FileRTFLoad As String = String.Empty
        Public Property FileRTFLoad() As String
            Get
                Return m_FileRTFLoad
            End Get
            Set(ByVal value As String)
                m_FileRTFLoad = value
                If Not String.IsNullOrEmpty(m_FileRTFLoad) Then
                    Me.LoadFile(m_FileRTFLoad, RichTextBoxStreamType.RichText)
                End If
            End Set
        End Property
        'prop sauve  rtf
        Private m_FileRTFSave As String = String.Empty
        Public Property FileRTFSave() As String
            Get
                Return m_FileRTFSave
            End Get
            Set(ByVal value As String)
                m_FileRTFSave = value
                If Not String.IsNullOrEmpty(m_FileRTFSave) Then
                    Me.SaveFile(m_FileRTFSave, RichTextBoxStreamType.RichText)
                End If
            End Set
        End Property
        Public Property EditingControlFormattedValue As Object Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlFormattedValue
            Get
     
                Return Me.Rtf
            End Get
            Set(ByVal value As Object)
     
                'Me.Rtf = value
                'MODIF
                If TypeOf value Is String Then
                    'modif
                    Me.Rtf = CStr(value)
                End If
     
            End Set
        End Property
        Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Rtf
        End Function
     
        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 Property EditingControlRowIndex As Integer Implements System.Windows.Forms.IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
        End Property
        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 Not dataGridViewWantsInputKey
            End Select
        End Function
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements System.Windows.Forms.IDataGridViewEditingControl.PrepareEditingControlForEdit
            ' No preparation needs to be done.
     
        End Sub
        Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements System.Windows.Forms.IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
        'MANQUANT : AJOUT
        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 System.Windows.Forms.IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = 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
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(e)
        End Sub
        'METHODES POUR SUB POUR CONTEXTMENU
        Private Sub SelColor()
            Dim MyDialog As New ColorDialog()
            MyDialog.AllowFullOpen = False
            MyDialog.ShowHelp = True
            MyDialog.Color = Me.ForeColor
     
            ' Update  if the user clicks OK 
            If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                Me.SelectionColor = MyDialog.Color
            End If
        End Sub
        Private Sub SelFont()
            Dim MyFont As New FontDialog()
            MyFont.FontMustExist = True
            MyFont.ShowHelp = True
            MyFont.Font = Me.Font
     
            ' Update  if the user clicks OK 
            If (MyFont.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                Me.SelectionFont = MyFont.Font
            End If
        End Sub
        Private Sub LoadRTF()
            Dim openDlg As New OpenFileDialog()
            openDlg.Title = "RTF files"
            openDlg.ShowHelp = True
            openDlg.Multiselect = False
            openDlg.Filter = "RTF Files(*.rtf)|*.rtf|Files Textes(*.txt)|*.txt"
     
            If (openDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                FileRTFLoad = openDlg.FileName
            End If
        End Sub
        Private Sub SaveRTF()
            Dim saveDlg As New SaveFileDialog()
            saveDlg.Title = "RTF files"
            saveDlg.ShowHelp = True
            saveDlg.CheckFileExists = True
            saveDlg.Filter = "RTF Files(*.rtf)|*.rtf|Files Textes(*.txt)|*.txt"
     
            If (saveDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                FileRTFSave = saveDlg.FileName
            End If
        End Sub
    End Class
    bon code....

  3. #3
    Membre émérite
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2012
    Messages
    337
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Vaucluse (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2012
    Messages : 337
    Par défaut
    Bonjour,

    Merci pour le temps que vous avez pris pour me répondre.

    J'ai modifié le code comme vous me l'avez proposé et cela fonctionne correctement.

    Il ne me reste plus qu'à trouver une solution pour afficher le texte formaté dans une cellule après édition car en l'état, j'ai une string avec les balises de formatage.
    En fouinant un peu sur le net, je suis tombé sur un exemple passant par une image pour afficher le texte. Le problème avec cette solution c'est qu'il n'est pas possible de récupérer le texte pour une utilisation ultérieure...

    Si quelqu'un à une idée, je suis preneur

    EDIT:
    Cette partie de code donne un résultat inatendu :
    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
     
    Public Class RichTextCell
        'Inherits DataGridViewCell
        'MODIF mauvais heritage
        Inherits DataGridViewTextBoxCell
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl As RichTextEditingControl =
                CType(DataGridView.EditingControl, RichTextEditingControl)
            'If (Me.Value Is Nothing) Then
            '    ctl.Rtf = CType(Me.DefaultNewRowValue, String)
            'Else
            '    ctl.Rtf = CType(Me.Value, String)
            'End If
     
     
            'MODIF 
            ' s'il n y a  pas de contenu rtf on se branche en mode texte
            If String.IsNullOrEmpty(ctl.Rtf) And Me.Value Is Nothing Then
                ctl.Text = CType(Me.DefaultNewRowValue, String)
            End If
            ' Sinon on laisse Base RichTextBox se charger en interne
            ' de recuperer le contenu rtf charge apres un appel RichTextBox.Loadfile...
     
        End Sub
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    If String.IsNullOrEmpty(ctl.Rtf) And Me.Value Is Nothing Then
        ctl.Text = CType(Me.DefaultNewRowValue, String)
    End If
    String.IsNullOrEmpty(ctl.Rtf) ne peut jamais être nul ou vide car ctl.Rtf contient dans tout les cas les balises de police.
    Du coup, si je laisse ce code, il copie le texte de la cellule précédente dans celle en cours d'édition. J'ai donc remis le code d'origine.

    Bon dev.

  4. #4
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    re

    Il ne me reste plus qu'à trouver une solution pour afficher le texte formaté dans une cellule après édition car en l'état, j'ai une string avec les balises de formatage.
    En fait c'est du à cette ligne qui doit renvoyer la valeur au format dgv qui est le string initial non formatte du dgv :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
     
     Public Function GetEditingControlFormattedValue(ByVal context As System.Windows.Forms.DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
            ' à remplacer par celle la 
            Return Me.Text
        End Function
    En fouinant un peu sur le net, je suis tombé sur un exemple passant par une image pour afficher le texte
    Je ne comprends pas le souci..
    Puisque le comportant du dgv "par design ms" est tel que sur Enter on entre dans le control d'edition (rtf ou datetimepicker) et on travaille dans le mode d'edition du control.
    Sur leave on quitte le control d'edition et on revient au mode d'edition par defaut du dgv...

    Je crois que modifier le dgv d'origine est loin d'etre facile...
    bon code...

  5. #5
    Membre émérite
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Décembre 2012
    Messages
    337
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France, Vaucluse (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2012
    Messages : 337
    Par défaut
    Je ne comprends pas le souci..
    Le soucis ne viens pas du contrôle d'édition mais du contrôle d'affichage.
    En cours d'édition, j'ai bien ma RichTextBox qui s'affiche pour saisir mon texte.
    C'est après la saisie que j'ai :
    "{\rtf1\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 abcd\par
    }"
    qui s'affiche si j'ai saisit "abcd".
    Ce que je souhaiterai c'est faire comme pour une DataGridViewComboBoxColumn ou une DataGridViewImageColomn ou les contrôles d'affichage sont respectivement une ComboBox et une PictureBox.

    Ceci vient probablement du fait que ma classe RichTextCell hérite de DataGridViewTextBoxCell.
    Il doit falloir passer par DataGridViewCell mais je ne trouve pas de propriété pour définir le type de contrôle pour l'affichage des données.

  6. #6
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    re

    Ce n'est pas du tout le comportement que j'ai chez moi....
    Sur Enter je peux taper un text en mode rtf que je peux formatter normalement....

    Sur Leave je reviens au mode d'edition normal et le texte "saisi" s'affiche normalement dans le font du dgv....

    Si je fais enter à nouveau le text repasse dans le mode rtf precedemment formatte....

    Tu dois avoir autre chose dans ton code........

    bon code....

Discussions similaires

  1. Personaliser dossier
    Par layouni dans le forum Windows
    Réponses: 1
    Dernier message: 23/08/2005, 12h05
  2. [debutant] Personalisation du confirm()
    Par glanumf dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 09/05/2005, 16h10
  3. personaliser Les boites de dialogue !!
    Par leo13 dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 19/01/2005, 18h36
  4. [FLASH MX] Personalisation d'un ScrollPane
    Par anksounamoun dans le forum Flash
    Réponses: 14
    Dernier message: 17/11/2004, 17h53

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