Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / Windows Forms

RichTextBox Cell in a DataGridView

Rate me:
Please Sign up or sign in to vote.
4.81/5 (53 votes)
9 Jul 2014CPOL2 min read 751K   11.6K   160   123
Source code for how to create a RichTextBox column in a DataGridView

DataGridViewRichTextBox

Introduction

You can use this DataGridViewRichTextBoxColumn to display and edit RTF content.

Background

A customer wanted to have superscript and subscript support in his reports, and he also wanted to edit the reports. I thought RTF files were a good choice. Then, a problem came up. I needed a setting dialog with a DataGridView, where the user will input text (should support superscript and subscript). So, I decided to put a RichTextBox in the DataGridView.

I found an idea here. There was no source code. So, I decided to write about it and post my first article in CodeProject.

Three Classes to make a DataGridViewColumn

There is an example of a calendar DataGridViewColumn here.

To make a custom DataGridViewColumn, we should write three classes:

  1. An editor control class derived from IDataGridViewEditingControl
  2. A cell class derived from DataGridViewCell or its descendant class
  3. A column class derived from DataGridViewColumn or its descendant class

First, the Editor Control Class

Here is the code for supporting multiline input in the RichTextBox:

C#
public class  DataGridViewRichTextBoxEditingControl : 
        RichTextBox, IDataGridViewEditingControl
{
    // To implement multiline, 'Enter' should be treated as an input key.
    protected override bool IsInputKey(Keys keyData)
    {
        Keys keys = keyData & Keys.KeyCode;

        if (keys == Keys.Return)
        {
            return this.Multiline;
        }

        return base.IsInputKey(keyData);
    } 

    // Tell DataGridView 'Enter' is an input key to this control.
    // And some other keys are also input keys.
    public bool EditingControlWantsInputKey
    (Keys keyData, bool dataGridViewWantsInputKey)
    {
        switch ((keyData & Keys.KeyCode))
        {
            case Keys.Return:
                // The code for 'Enter' is copied from 
                // DataGridViewTextBoxEditingControl,
                // Shift + Enter = NewLine
                if ((((keyData & (Keys.Alt | Keys.Control | Keys.Shift))
                     == Keys.Shift) && this.Multiline))
                {
                    return true;
                }
                break;
            case Keys.Left:
            case Keys.Right:
            case Keys.Up:
            case Keys.Down:
                return true;
        }

        return !dataGridViewWantsInputKey;
    }
}

Here is the code for shortcuts. RichTextBox already supports Ctrl + '+' and Ctrl + Shift + '+' for subscript and superscript. What needs to be done is add support for Ctrl + 'B', Ctrl + 'I', and Ctrl + 'U'.

C#
protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);

    if (e.Control)
    {
        switch (e.KeyCode)
        {
            // Control + B = Bold
            case Keys.B:
                if (this.SelectionFont.Bold)
                {
                    this.SelectionFont = new Font(this.Font.FontFamily, 
                     this.Font.Size, ~FontStyle.Bold & this.Font.Style);
                }
                else
                    this.SelectionFont = new Font(this.Font.FontFamily, 
                     this.Font.Size, FontStyle.Bold | this.Font.Style);
                break;
            // Control + U = Underline
            case Keys.U:
                if (this.SelectionFont.Underline)
                {
                    this.SelectionFont = new Font(this.Font.FontFamily, 
                     this.Font.Size, ~FontStyle.Underline & this.Font.Style);
                }
                else
                    this.SelectionFont = new Font(this.Font.FontFamily, 
                     this.Font.Size, FontStyle.Underline | this.Font.Style);
                break;
            // Control + I = Italic
            // Conflicts with the default shortcut
            //case Keys.I:
            //    if (this.SelectionFont.Italic)
            //    {
            //        this.SelectionFont = new Font(this.Font.FontFamily, 
            //         this.Font.Size, ~FontStyle.Italic & this.Font.Style);
            //    }
            //    else
            //        this.SelectionFont = new Font(this.Font.FontFamily, 
            //         this.Font.Size, FontStyle.Italic | this.Font.Style);
            //    break;
            default:
                break;
        }
    }
}

Second, the Cell Class

This is the most important class for RichTextBoxColumn, because here we do the painting job for the cell.

I suggest you should first take a look here. Then, you will know how to print a RichTextBox. I just changed it a little to print it to an Image object.

The cell class was derived from DataGridViewTextBoxCell due to speed problems. I changed it with DataGridViewImageCell. I needed to do something to avoid errors in the DataGridViewImageCell.

C#
public class DataGridViewRichTextBoxCell : DataGridViewImageCell
{
    // The value should be RTF string, so these types should be changed.	
    public override Type ValueType
    {
        get
        {
            return typeof(string);
        }
        set
        {
            base.ValueType = value;
        }
    }

    public override Type FormattedValueType
    {
        get
        {
            return typeof(string);
        }
    } 

    // Since the value type is changed, we need to do something more.
    private static void SetRichTextBoxText(RichTextBox ctl, string text)
    {
        try
        {
            ctl.Rtf = text;
        }
        catch (ArgumentException)
        {
            ctl.Text = text;
        }
    }
	
    public override void InitializeEditingControl
	(int rowIndex, object initialFormattedValue, 
	DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl
	(rowIndex, initialFormattedValue, dataGridViewCellStyle);

        RichTextBox ctl = DataGridView.EditingControl as RichTextBox;

        if (ctl != null)
        {
            SetRichTextBoxText(ctl, Convert.ToString(initialFormattedValue));
        }
    }

    protected override object GetFormattedValue
	(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, 
	    TypeConverter valueTypeConverter, 
		TypeConverter formattedValueTypeConverter,
		 DataGridViewDataErrorContexts context)
    {
        return value;
    }
}

Now, we will paint the cell.

The RichTextBoxPrinter.Print function is from the link I mentioned above.

C#
private static readonly RichTextBox _editingControl = new RichTextBox();

// Images for selected and normal states.
private Image GetRtfImage(int rowIndex, object value, bool selected)
{
    Size cellSize = GetSize(rowIndex);

    if (cellSize.Width < 1 || cellSize.Height < 1)
        return null;

    RichTextBox ctl = null;

    if (ctl == null)
    {
        ctl = _editingControl;
        ctl.Size = GetSize(rowIndex);
        SetRichTextBoxText(ctl, Convert.ToString(value));
    }

    if (ctl != null)
    {
        // Print the content of RichTextBox to an image.
        Size imgSize = new Size(cellSize.Width - 1, cellSize.Height - 1);
        Image rtfImg = null;

        if (selected)
        {
            // Selected cell state
            ctl.BackColor = DataGridView.DefaultCellStyle.SelectionBackColor;
            ctl.ForeColor = DataGridView.DefaultCellStyle.SelectionForeColor;

            // Print image
            rtfImg = RichTextBoxPrinter.Print(ctl, imgSize.Width, imgSize.Height);

            // Restore RichTextBox
            ctl.BackColor = DataGridView.DefaultCellStyle.BackColor;
            ctl.ForeColor = DataGridView.DefaultCellStyle.ForeColor;
        }
        else
        {
            rtfImg = RichTextBoxPrinter.Print(ctl, imgSize.Width, imgSize.Height);
        }

        return rtfImg;
    }

    return null;
}

// Draw the image of the rich text box
protected override void Paint(Graphics graphics, 
	Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
    DataGridViewElementStates cellState, object value, 
	object formattedValue, string errorText, 
    	DataGridViewCellStyle cellStyle, 
	DataGridViewAdvancedBorderStyle advancedBorderStyle, 
	DataGridViewPaintParts paintParts)
{
    base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
	cellState, null, null, errorText, cellStyle, advancedBorderStyle, paintParts);

    Image img = GetRtfImage(rowIndex, value, base.Selected);

    if (img != null)
        graphics.DrawImage(img, cellBounds.Left, cellBounds.Top);
}

Other things required for cell edit:

C#
// Remember, DataGridViewImageCell doesn't behave like DataGridViewTextBoxCell.
// So we need to handle the mouse events for edit.

#region Handlers of edit events, copied from DataGridViewTextBoxCell 

private byte flagsState;

protected override void OnEnter(int rowIndex, bool throughMouseClick)
{
    base.OnEnter(rowIndex, throughMouseClick);

    if ((base.DataGridView != null) && throughMouseClick)
    {
        this.flagsState = (byte)(this.flagsState | 1);
    }
}

protected override void OnLeave(int rowIndex, bool throughMouseClick)
{
    base.OnLeave(rowIndex, throughMouseClick);

    if (base.DataGridView != null)
    {
        this.flagsState = (byte)(this.flagsState & -2);
    }
}

protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
    base.OnMouseClick(e);
    if (base.DataGridView != null)
    {
        Point currentCellAddress = base.DataGridView.CurrentCellAddress;

        if (((currentCellAddress.X == e.ColumnIndex) && 
            (currentCellAddress.Y == e.RowIndex)) && 
            (e.Button == MouseButtons.Left))
        {
            if ((this.flagsState & 1) != 0)
            {
                this.flagsState = (byte)(this.flagsState & -2);
            }
            else if (base.DataGridView.EditMode != 
                      DataGridViewEditMode.EditProgrammatically)
            {
                base.DataGridView.BeginEdit(false);
            }
        }
    }
}

#endregion

Finally, the Column Class

It's easy because we already have the classes for the editor control and the cell:

C#
public class DataGridViewRichTextBoxColumn : DataGridViewColumn
{
    public DataGridViewRichTextBoxColumn()
        : base(new DataGridViewRichTextBoxCell())
    { 
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            if (!(value is DataGridViewRichTextBoxCell))
                throw new InvalidCastException("CellTemplate must" + 
                      " be a DataGridViewRichTextBoxCell");

            base.CellTemplate = value;
        }
    }
}

Using the Code

Use it just like other columns for DataGridView. See the demo source code.

Points of Interest

At first, the cell class was derived from DataGridViewTextBoxCell. It's OK if the text is short. But, when the RTF content was long or it contained a picture, it became slow when entering or leaving the cell. Then, I changed the parent class to DataGridViewImageCell, and now the speed is no longer a problem.

History

  • 7/19/2014
    • Add some sample code for ashsanD to demostrate how to set the specific text bold. E.g. user can search some text in the datagridview and the matched text will be highlighted.
  • 4/30/2009
    • Updated class DataGridViewRichTextBoxCell (including gzobi666's correction)
    • Updated RichTextBoxPrinter according to the D_Kondrad's correction
  • 12/18/2008
    • Added the code with instructions

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThere is an error in using your program Pin
hambor18-Jun-11 3:34
hambor18-Jun-11 3:34 
GeneralThanks Pin
YZK30-Mar-11 0:25
YZK30-Mar-11 0:25 
GeneralMy vote of 5 Pin
DhavalShah198812328-Dec-10 20:44
DhavalShah198812328-Dec-10 20:44 
GeneralRight To Left Pin
alireza.noorian28-Aug-10 19:29
alireza.noorian28-Aug-10 19:29 
GeneralRe: Right To Left Pin
mrwisdom29-Aug-10 14:22
mrwisdom29-Aug-10 14:22 
GeneralClickable Web Links Pin
flippydeflippydebop23-Jul-10 6:13
flippydeflippydebop23-Jul-10 6:13 
QuestionHow to change the text data to rtf byte data for database Pin
y-oguri@cty-net.ne.jp2-Jul-10 1:33
y-oguri@cty-net.ne.jp2-Jul-10 1:33 
QuestionHow to change Text color Pin
y-oguri@cty-net.ne.jp2-Jul-10 1:05
y-oguri@cty-net.ne.jp2-Jul-10 1:05 
Thank you every one. The Code project makes me happy.

I'me very happy to up my idea for changing to text color.

Step(1) Please confirm changing color

protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);

if (e.Control)
{
switch (e.KeyCode)
{
// Control + B = Bold
case Keys.B:
if (this.SelectionFont.Bold)
{
this.SelectionFont = new Font(this.Font.FontFamily,
this.Font.Size, ~FontStyle.Bold & this.Font.Style);
}
else
this.SelectionFont = new Font(this.Font.FontFamily,
this.Font.Size, FontStyle.Bold | this.Font.Style);

this.SelectionColor=Color.Pink;

break;

Step(2)

make static mcolor
public class DataGridViewRichTextBoxEditingControl : RichTextBox, IDataGridViewEditingControl
{
private DataGridView _dataGridView;
private int _rowIndex;
private bool _valueChanged;
public static Color mcolor = Color.Black;

it is possible to change the text color from your applicationlike below

private void ********_SelectedColorChanged(object sender, EventArgs e)
{
DataGridViewRichTextBox.DataGridViewRichTextBoxEditingControl.mcolor = *****.Color;

}
AnswerUnderline and Bold together in the DataGridView Pin
Bigdeak14-Jun-10 4:02
Bigdeak14-Jun-10 4:02 
QuestionMargin/Padding Pin
cschom19-May-10 6:09
cschom19-May-10 6:09 
AnswerRe: Margin/Padding Pin
mrwisdom19-May-10 15:40
mrwisdom19-May-10 15:40 
GeneralRe: Margin/Padding Pin
cschom20-May-10 3:16
cschom20-May-10 3:16 
GeneralDon't work DataGridView.AutoSizeRows Pin
Panikovsky11-Apr-10 22:02
Panikovsky11-Apr-10 22:02 
AnswerRe: Don't work DataGridView.AutoSizeRows Pin
mrwisdom12-Apr-10 15:25
mrwisdom12-Apr-10 15:25 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
Panikovsky14-Apr-10 1:02
Panikovsky14-Apr-10 1:02 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
flippydeflippydebop24-Jul-10 6:45
flippydeflippydebop24-Jul-10 6:45 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
Panikovsky25-Jul-10 19:35
Panikovsky25-Jul-10 19:35 
QuestionRe: Don't work DataGridView.AutoSizeRows Pin
fivetee_cp29-Apr-11 13:06
fivetee_cp29-Apr-11 13:06 
AnswerRe: Don't work DataGridView.AutoSizeRows Pin
Panikovsky2-May-11 17:56
Panikovsky2-May-11 17:56 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
fivetee_cp3-May-11 22:01
fivetee_cp3-May-11 22:01 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
psymon2520-Oct-10 1:47
psymon2520-Oct-10 1:47 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
Panikovsky20-Oct-10 19:22
Panikovsky20-Oct-10 19:22 
GeneralRe: Don't work DataGridView.AutoSizeRows Pin
psymon2521-Oct-10 4:16
psymon2521-Oct-10 4:16 
QuestionHow To Change the cell color Pin
YokoToyama22-Oct-09 15:16
YokoToyama22-Oct-09 15:16 
AnswerRe: How To Change the cell color Pin
mrwisdom25-Oct-09 16:13
mrwisdom25-Oct-09 16:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.