Example E2503
Visible to All Users

WinForms Data Grid - Display rich text in data cells and edit cell values in a popup window

This example demonstrates how to use different data editors to display and edit cell values. The RichEditControl is used to edit RTF data (it is displayed within a popup window). The RepositoryItemRichTextEdit is used to display RTF data in data cells.

The CustomRowCellEditForEditing event is handled to assign a custom RTF editor to a column for in-place editing (to override the default column editor):

C#
void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column.FieldName == "Rich") e.RepositoryItem = riPopup; }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
// Developer Express Code Central Example: // How to display rich text in GridView's cells, but edit their content in a popup window // // This example demonstrates how to provide different editors for displaying and // editing data. In this scenario, cells values can be edited using the // RichEditControl located in the popup window. However, the // RepositoryItemRichTextEdit is used to display cells content when their in-place // editor is closed. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E2503 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors.Repository; using DevExpress.XtraEditors; using DevExpress.XtraRichEdit; using DevExpress.XtraEditors.Controls; namespace WindowsApplication1 { public partial class Form1 : Form { private DataTable CreateTable(int RowCount) { DataTable tbl = new DataTable(); tbl.Columns.Add("Name", typeof(string)); tbl.Columns.Add("ID", typeof(int)); tbl.Columns.Add("Number", typeof(int)); tbl.Columns.Add("Date", typeof(DateTime)); tbl.Columns.Add("Rich", typeof(string)); for (int i = 0; i < RowCount; i++) tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i), RTFTextProvider.GetRichText(i) }); return tbl; } public Form1() { InitializeComponent(); gridControl1.DataSource = CreateTable(20); gridView1.OptionsView.RowAutoHeight = true; gridView1.RowHeight = 30; gridColumn1.Width = 20; riPopup.PopupFormMinSize = new Size(400, 400); } void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column.FieldName == "Rich") e.RepositoryItem = riPopup; } void riPopup_QueryPopUp(object sender, CancelEventArgs e) { BaseEdit editor = (BaseEdit)sender; richEditControl.Document.RtfText = editor.EditValue.ToString(); } void riPopup_QueryDisplayText(object sender, QueryDisplayTextEventArgs e) { e.DisplayText = richEditControl.Document.Text; } void riPopup_QueryResultValue(object sender, QueryResultValueEventArgs e) { e.Value = richEditControl.Document.RtfText; } private void riPopup_CloseUp(object sender, CloseUpEventArgs e) { if(!e.AcceptValue) { PopupContainerEdit pSender = (PopupContainerEdit)sender; RichEditControl rEdit = (RichEditControl)pSender.Properties.PopupControl.Controls[0]; rEdit.Document.RtfText = e.Value.ToString(); } } } }
RTFTestProvider.cs(vb)
C#
// Developer Express Code Central Example: // How to display rich text in GridView's cells, but edit their content in a popup window // // This example demonstrates how to provide different editors for displaying and // editing data. In this scenario, cells values can be edited using the // RichEditControl located in the popup window. However, the // RepositoryItemRichTextEdit is used to display cells content when their in-place // editor is closed. // // You can find sample updates and versions for different programming languages here: // http://www.devexpress.com/example=E2503 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsApplication1 { public static class RTFTextProvider { private static string _TestText; static RTFTextProvider() { StreamReader tr = new StreamReader("..\\..\\RTFText.rtf"); _TestText = tr.ReadToEnd(); } public static object GetRichText(object obj) { return _TestText.Replace("TextToReplace", obj.ToString()); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.