How to: Filter Columns that Display RTF Data

  • 4 minutes to read

In this example, a GridControl contains a Description column that displays RTF data in its cells (using an RichTextBox in-place editor).

By default, when displaying the column's filter dropdown list, this list will contain RTF text, including RTF specifiers, making the text human-unreadable.This issue can be resolved as shown in the example.A helper unbound column is created that will be used as a substitute when the filtering functionality is invoked for the Description column.

The unbound column's values will be the Description column's values converted to simple text.

When an end-user invokes a filter dropdown list for the Description column, the list will actually display values of the created unbound column.Here are the steps to implement this approach:1) A new unbound column (field name:"SimpleText") is created. This column is hidden by setting its Visible and ShowInCustomizationForm properties to false.

2) The SimpleText column's caption is set to the same value as for the Description column, so a user will not notice data replacement.

3) The UnboundColumnData event is handled to populate the SimpleText column with data. The column's values will be the Description column's values converted to simple text.

4) The Description column's FieldNameSortGroup property is set to the unbound column's field name ("SimpleText"). This forces the grid to use values of the SimpleText column when sorting/grouping/filtering is applied to the Description column.

5) Set the Description column's OptionsFilter.FilterBySortField property to DevExpress.Utils.DefaultBoolean.True

View Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace E2907 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            InitData();
        }

        void InitData() {
            string[] descriptions = new string[] {"General description", 
                @"{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}{\f1 Arial;}}{\colortbl\red0\green0\blue0;\red49\green105\blue198;\red255\green0\blue0;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}{\*\cs1\cf0 Default Paragraph Font;}}{\sectd\pard\plain\ql{\b\f1\fs16\cf0 BOLD }{\f1\fs16\cf0 description}\par}}",
                @"{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}{\f1 Arial;}}{\colortbl\red0\green0\blue0;\red49\green105\blue198;\red255\green0\blue0;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}{\*\cs1\cf0 Default Paragraph Font;}}{\sectd\pard\plain\ql{\f1\fs16\cf0 Description with }{\ul\f1\fs16\cf0 UNDERLINE }{\f1\fs16\cf0 text}\par}}", 
                @"{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}{\f1 Arial;}}{\colortbl\red0\green0\blue0;\red49\green105\blue198;\red255\green0\blue0;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}{\*\cs1\cf0 Default Paragraph Font;}}{\sectd\pard\plain\ql{\i\f1\fs16\cf0 ITALIC }{\f1\fs16\cf0 description}\par}}"};
            DataTable tbl = new DataTable();
            tbl.Columns.Add("ID", typeof(int));
            tbl.Columns.Add("Name", typeof(string));
            tbl.Columns.Add("Description", typeof(string));
            for(int i = 1; i < 10; i++)
                tbl.Rows.Add(i, string.Format("Item{0}", i), descriptions[i%4]);
            gridControl1.DataSource = tbl;
        }

        private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
            if(e.Column.FieldName == "SimpleText" && e.IsGetData) {
                object value = ((GridView)sender).GetListSourceRowCellValue(e.ListSourceRowIndex, colDescription);
                e.Value = repositoryItemRichTextEdit1.ConvertEditValueToPlainText(value);
            }
        }
    }
}