New to Telerik UI for WinForms? Download free 30-day trial

How to create GridViewMarkupEditorColumn

Environment

Product Version Product Author
2021.1.223 RadGridView for WinForms Nadya Karaivanova

Description

A common requirement is to have a column in RadGridView that allows text formatting such as bold, italics, underline, link, etc. The following example demonstrates how you can achieve such custom column that uses RadMarkupEditor. Once, you enter the cell in edit mode the dialog is shown and you are able to format the text according to your needs.

gridviewmarkupeditorcolumn.png

As of R1 2023 the Microsoft.mshtml assembly is excluded from Telerik.WinControls.RadMarkupEditor.dll. For .NET Core (.NET 6, .NET 7 or newer) projects, it is necessary to add the reference explicitly to use RadMarkupupEditor since it uses the MS WebBrowser internally.

Solution

RadMarkupEditor is a dialog used to create a formatted text and is suitable to be used as an editor within GridViewColumn.

You should have reference to Telerik.WinControls.RadMarkupEditor assembly in your project.

Let's first create our custom GridViewMarkupEditor that inherits from the BaseGridEditor:

public class GridViewMarkupEditor : BaseGridEditor
{
    RadMarkupDialog markupDialog;

    public GridViewMarkupEditor()
    {
    }

    protected override RadElement CreateEditorElement()
    {
        return new RadLabelElement();
    }

    public override object Value
    {
        get
        {
            GridDataCellElement dataCell = this.OwnerElement as GridDataCellElement;
            return dataCell.Value;
        }
        set
        {
            GridDataCellElement dataCell = this.OwnerElement as GridDataCellElement;
            dataCell.Value = value;
        }
    }

    public override void BeginEdit()
    {
        base.BeginEdit();

        RadMarkupDialog dialog = new RadMarkupDialog();
        dialog.Value = Convert.ToString(this.Value);

        DialogResult result = dialog.ShowDialog();

        if (result == DialogResult.OK)
        {
            this.Value = dialog.Value;
        }
    }

    public override bool EndEdit()
    {
        return base.EndEdit();
    }
}

Second, we should create our custom GridViewMarkupColumn that will use the custom GridViewMarkupEditor:

public class GridViewMarkupColumn : GridViewDataColumn
{
    public GridViewMarkupColumn(string fieldName)
        : base(fieldName)
    {

    }
    public GridViewMarkupColumn(string uniqueName, string fieldName)
        : base(uniqueName, fieldName)
    {
    }

    protected override void Initialize()
    {
        base.Initialize();
        this.DisableHTMLRendering = false;
    }


    public override Type GetDefaultEditorType()
    {
        return typeof(GridViewMarkupEditor);
    }

    public override IInputEditor GetDefaultEditor()
    {
        return new GridViewMarkupEditor();
    }
}

Then, we can use the custom column in the RadGridView:

public Form3GridMarkup()
{
    InitializeComponent();
    this.radGridView1.Columns.Add(new GridViewMarkupColumn("RichText", "RichText"));
    this.radGridView1.Columns[0].Width = 250;

    string[] colors = new string[] { "LightCyan", "Yellow", "YellowGreen", "LightBlue", "Pink" };
    for (int i = 0; i < 50; i++)
    {
        GridViewRowInfo row = radGridView1.Rows.AddNew();
        row.Cells[0].Value = string.Format("<html><span style=\"background-color:{0}\">Highlighted Text {1}</span></html>", colors[i % 5], i);
    }
}
In this article