You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用RichTextEdit1实现GridControl中文本的格式编辑?

Text Formatting for DevExpress GridControl in Your WinForms App

Hey there, let's tackle that text formatting issue you're facing with your GridControl connected to the SQL Server table. First, let's recap your setup to make sure we're on the same page:

Your Environment & Setup

  • Windows 7 x64
  • Visual Studio 2015 x64
  • DevExpress 17.1.5
  • MSSql Server 2014
  • Target table structure:
CREATE TABLE "tbl0410Categories" ( 
    "CategoryId" INT NOT NULL, 
    "Name" NVARCHAR(max) NOT NULL, 
    "fotoLink" VARCHAR(1000) NULL DEFAULT NULL, 
    "text1" VARCHAR(7000) NULL DEFAULT NULL, 
    PRIMARY KEY ("CategoryId") 
)

You mentioned you've connected the GridControl to this table but aren't sure how to format the text (especially the text1 field), and you suspect you need an extra formatting panel. You're right—DevExpress has built-in tools to handle this, and here are two straightforward ways to set it up:


Option 1: Embed a Rich Text Editor Directly in the Grid Column

If you want to edit and view formatted text right inside the grid cells, use DevExpress's RepositoryItemRichTextEdit as the column editor:

  1. Open your WinForm in the Visual Studio Designer, then select your GridControl.
  2. Click the small arrow in the top-right corner to open the GridControl Designer.
  3. Go to the Columns tab, find your text1 column.
  4. In the column's properties, look for ColumnEdit—click the dropdown, then select New Repository Item > RichTextEdit.
  5. Make sure the column's OptionsColumn.AllowEdit is set to true if you want users to modify the formatted text.
  6. Now, when you run the app, the text1 cells will render rich text (bold, italic, lists, etc.) and let you edit it with basic formatting tools right in the grid.

Option 2: Add a Standalone Formatting Panel (Full Toolbar)

If you want a dedicated, full-featured formatting toolbar (like what you'd see in Word), pair a RichEditControl with a RichEditToolbar on your form:

  1. Drag both a RichEditControl and a RichEditToolbar from the DevExpress Toolbox onto your WinForm.
  2. Link the toolbar to the editor: select the toolbar, go to its properties, and set RichEditControl to your RichEditControl instance.
  3. Add code to load the selected row's text1 content into the editor:
    private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
        if (gridView == null || gridView.FocusedRowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
            return;
        
        // Load the text content—use RtfText if storing formatted content, or Text for plain text
        var textContent = gridView.GetFocusedRowCellValue("text1")?.ToString() ?? string.Empty;
        richEditControl1.RtfText = textContent;
    }
    
  4. Add a save button (or use your existing save logic) to push the formatted content back to the grid and database:
    private void btnSaveFormat_Click(object sender, EventArgs e)
    {
        var gridView = gridView1;
        if (gridView.FocusedRowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
            return;
        
        // Update the grid cell with the formatted RTF content
        gridView.SetFocusedRowCellValue("text1", richEditControl1.RtfText);
        // Don't forget to save changes to your SQL Server table here (using your data context or SQL commands)
    }
    

Quick Notes for DevExpress 17.1.5

  • Both RepositoryItemRichTextEdit and RichEditToolbar are fully supported in your version, no need to upgrade.
  • If you're currently storing plain text in text1 and just want to apply display-only formatting (like font color or bold for certain conditions), you can use GridView's Conditional Formatting feature—but that's limited to basic styles. For full rich text editing, the options above are better.

内容的提问来源于stack exchange,提问作者climivin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:13:58