Advertisement

05.08.2008 at 01:06AM PDT, ID: 23385336
[x]
Attachment Details

How do I add a Richtextbox to a Datagridview?

Asked by BOIT in Microsoft Visual Basic.Net, .NET Framework 2.x, .NET Framework 3.x versions

Tags:

I need to display and edit rich text data in a datagridview on a form.

Researching the subject it seems that I need to create a custom column and I have attempted to adapt Microsoft's example for a dattimepicker.

Problem 1: When the data is displayed it shows plain text with the various rich text control characters instead of formatted text (e.g.  {rtf1\ansi\ansicpg 1252\  etc..). However, when I click on the box the editor then displays the rich text correctly.

Problem 2: I have developed a context menu for a rich text box that I use on free standing rich text boxes to allow user to cut,copy,paste and access the font dialog to make text bold, underline etc. This works fine. But I do I attach it to a rich text box hosted in a datagridviewcell?

Any help on these two would be much appreciated?

Code attachedStart Free Trial
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:
Public Class BoitDataGridViewColumnRichText : Inherits DataGridViewColumn
    Public Sub New()
        CellTemplate = New DataGridViewRichTextCell
    End Sub
    Public Overrides Property CellTemplate() As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(ByVal value As DataGridViewCell)
            If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(DataGridViewRichTextCell)) Then
                Throw New InvalidCastException("Must be a DataGridViewRichTextCell")
            End If
            MyBase.CellTemplate = value
        End Set
    End Property
End Class
Public Class DataGridViewRichTextCell
    Inherits DataGridViewTextBoxCell
    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
          ByVal initialFormattedValue As Object, _
          ByVal dataGridViewCellStyle As DataGridViewCellStyle)
 
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
        Dim ctl As RichTextEditingControl = CType(DataGridView.EditingControl, RichTextEditingControl)
        ctl.Rtf = CType(Me.Value, String)
    End Sub
    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(RichTextEditingControl)
        End Get
    End Property
    Public Overrides ReadOnly Property ValueType() As Type
        Get
            Return GetType(String)
        End Get
    End Property
    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            Return String.Empty
        End Get
    End Property
End Class
Class RichTextEditingControl
    Inherits RichTextBox
    Implements IDataGridViewEditingControl
 
    Private dataGridViewControl As DataGridView
    Private valueIsChanged As Boolean = False
    Private rowIndexNum As Integer
    Public Sub New()
        Me.MaxLength = 2000
        Me.Multiline = True
        Me.Font = New Font("Arial", 10)
    End Sub
    Public Property EditingControlFormattedValue() As Object _
        Implements IDataGridViewEditingControl.EditingControlFormattedValue
 
        Get
            Return Me.Text
        End Get
        Set(ByVal value As Object)
            If TypeOf value Is String Then
                Me.Text = CType(value, String)
            End If
        End Set
    End Property
    Public Function GetEditingControlFormattedValue(ByVal context _
        As DataGridViewDataErrorContexts) As Object _
        Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
 
        Return Me.Text
    End Function
    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As  _
        DataGridViewCellStyle) _
        Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
 
        Me.Font = dataGridViewCellStyle.Font
    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
 
        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 Not dataGridViewWantsInputKey
        End Select
 
    End Function
 
    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
        Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
 
    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
 
End Class
 
Loading Advertisement...
 
[+][-]05.08.2008 at 02:05AM PDT, ID: 21523273

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.08.2008 at 02:28AM PDT, ID: 21523340

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 04:22AM PDT, ID: 21667947

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, .NET Framework 2.x, .NET Framework 3.x versions
Tags: vb.net
Sign Up Now!
Solution Provided By: BOIT
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.29.2008 at 05:19AM PDT, ID: 21668280

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628