Back to Devexpress

How to: Filter Columns that Display RTF Data

windowsforms-9346-controls-and-libraries-data-grid-examples-filtering-how-to-filter-columns-that-display-rtf-data.md

latest7.6 KB
Original Source

How to: Filter Columns that Display RTF Data

  • Nov 13, 2018
  • 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.

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

  2. 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.

  3. 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.

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

csharp
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);
            }
        }
    }
}
csharp
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace E2907 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms

Namespace E2907
    Friend NotInheritable Class Program
        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        Private Sub New()
        End Sub
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())
        End Sub
    End Class
End Namespace
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid

Namespace E2907
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            InitData()
        End Sub

        Private Sub InitData()
            Dim descriptions() As 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}}"}
            Dim tbl As New DataTable()
            tbl.Columns.Add("ID", GetType(Integer))
            tbl.Columns.Add("Name", GetType(String))
            tbl.Columns.Add("Description", GetType(String))
            For i As Integer = 1 To 9
                tbl.Rows.Add(i, String.Format("Item{0}", i), descriptions(i Mod 4))
            Next i
            gridControl1.DataSource = tbl
        End Sub

        Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles gridView1.CustomUnboundColumnData
            If e.Column.FieldName = "SimpleText" AndAlso e.IsGetData Then
                Dim value As Object = (CType(sender, GridView)).GetListSourceRowCellValue(e.ListSourceRowIndex, colDescription)
                e.Value = repositoryItemRichTextEdit1.ConvertEditValueToPlainText(value)
            End If
        End Sub
    End Class
End Namespace