# Rendering TX Text Control's Content to a DataGridViewCell

> Standard .NET DataGridView cells provide only basic rich text support. This sample uses the CellPainting event with TX Text Control's DrawToBitmap method to render fully formatted document content as a GDI+ bitmap image inside a DataGridViewCell without hosting the control.

- **Author:** Bjoern Meyer
- **Published:** 2009-01-20
- **Modified:** 2026-07-17
- **Description:** Standard .NET DataGridView cells provide only basic rich text support. This sample uses the CellPainting event with TX Text Control's DrawToBitmap method to render fully formatted document content as a GDI+ bitmap image inside a DataGridViewCell without hosting the control.
- **2 min read** (299 words)
- **Tags:**
  - Data Grid
  - .NET
  - Sample
- **Web URL:** https://www.textcontrol.com/blog/2009/01/20/rendering-tx-text-controls-content-to-a-datagridviewcell/
- **LLMs URL:** https://www.textcontrol.com/blog/2009/01/20/rendering-tx-text-controls-content-to-a-datagridviewcell/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2009/01/20/rendering-tx-text-controls-content-to-a-datagridviewcell/llms-full.txt

---

The rich text support of the .NET *DataGridView* or other commercial components are very basic and in most cases limited to the features of the Microsoft *RichTextBox*.

To enable users to display more advanced rich content in a *DataGridViewCell*, you can render the text of *TXTextControl.TextControl* to the *Graphics* object of a specific cell.

![Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2009/01/20/a/assets/tx_sample_grid.webp "Text Control") There are also possibilities to insert the control itself to the cell when it is activated. You can build your own *DataGridViewColumn* that adds another control to the cell. This project shows how to achieve this:

[RichTextBox cell in a DataGridView](http://www.codeproject.com/KB/grid/RtfInDataGridView.aspx)

Anyway, this sample code shows only how to render the content of TX Text Control to the cell. I am using the CellPainting event of the DataGridView to change the painting. We are just drawing the background of the cell. The *e.Handled = true;* statement avoids further painting of the cell.

The *DrawToBitmap* method of TX Text Control is used to create an image of the content which is rendered using the GDI+ *DrawImage* method. It is a very easy, but effective way to provide rich text content in a data grid.

```
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    // hardcoded cell index
    if (e.ColumnIndex == 2 && e.RowIndex == 1)
    {
        // draw the background and disable the drawing
        e.PaintBackground(e.ClipBounds, true);
        e.Handled = true;

        // set the size of the cell
        dataGridView1.Rows[1].Height = textControl1.Height;
        dataGridView1.Columns[2].Width = textControl1.Width;

        // create the bitmap
        Bitmap bmp = new Bitmap(textControl1.Width, textControl1.Height);
        textControl1.DrawToBitmap(bmp, new Rectangle(0, 0,
            textControl1.Width, textControl1.Height));

        // draw the bitmap to the cell
        e.Graphics.DrawImage(bmp, new Point(e.CellBounds.X, e.CellBounds.Y));
    }
}
```

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Create a Table of Contents in Windows Forms using C#](https://www.textcontrol.com/blog/2023/01/23/create-toc-in-windows-forms/llms.txt)
- [Two Ways to Restart Numbered Lists in TX Text Control](https://www.textcontrol.com/blog/2021/11/03/two-ways-to-restart-numbered-lists/llms.txt)
- [Paste Special: The Easy Way to Implement](https://www.textcontrol.com/blog/2009/09/07/paste-special-the-easy-way-to-implement/llms.txt)
- [How to Remove All Section Breaks in a Document?](https://www.textcontrol.com/blog/2009/05/14/how-to-remove-all-section-breaks-in-a-document/llms.txt)
- [Batch Printing: How to Print Documents in One Print Job](https://www.textcontrol.com/blog/2009/03/20/batch-printing-how-to-print-documents-in-one-print-job/llms.txt)
- [Removing TextFields in a Loop](https://www.textcontrol.com/blog/2009/03/12/removing-textfields-in-a-loop/llms.txt)
- [Creating a Table of Contents (TOC) Using TX Text Control](https://www.textcontrol.com/blog/2009/02/02/creating-a-table-of-contents-toc-using-tx-text-control/llms.txt)
- [Revised Sample: Converting Text to a Table](https://www.textcontrol.com/blog/2009/01/07/revised-sample-converting-text-to-a-table/llms.txt)
- [New Sample: Printing to Different Paper Trays](https://www.textcontrol.com/blog/2008/10/08/new-sample-printing-to-different-paper-trays/llms.txt)
- [Is the Current Input Position Currently Visible?](https://www.textcontrol.com/blog/2008/09/04/is-the-current-input-position-currently-visible/llms.txt)
- [New Sample: Implementing a 'paste Special' Dialog Box](https://www.textcontrol.com/blog/2008/07/10/new-sample-implementing-a-paste-special-dialog-box/llms.txt)
- [The ParagraphStyle and InlineStyle Constructor Unreveled](https://www.textcontrol.com/blog/2008/06/24/the-paragraphstyle-and-inlinestyle-constructor-unreveled/llms.txt)
- [Searching with C# Escape Sequences](https://www.textcontrol.com/blog/2008/05/29/searching-with-c-escape-sequences/llms.txt)
- [Removing Empty Tables](https://www.textcontrol.com/blog/2008/05/27/removing-empty-tables/llms.txt)
- [Manipulating the Selection Object](https://www.textcontrol.com/blog/2008/05/26/manipulating-the-selection-object/llms.txt)
- [Casting TextField Derivatives](https://www.textcontrol.com/blog/2008/05/23/casting-textfield-derivatives/llms.txt)
- [Resize Images to Fit into Page](https://www.textcontrol.com/blog/2008/05/08/resize-images-to-fit-into-page/llms.txt)
- [Printing into a PrintPreview Control](https://www.textcontrol.com/blog/2007/08/10/printing-into-a-printpreview-control/llms.txt)
- [Drag And Drop Images into TX Text Control .NET for Windows Forms](https://www.textcontrol.com/blog/2007/08/09/drag-and-drop-images-into-tx-text-control-net-for-windows-forms/llms.txt)
- [New Sample: Restarting a Numbered List](https://www.textcontrol.com/blog/2007/08/08/new-sample-restarting-a-numbered-list/llms.txt)
- [Is There a Table at the Current Mouse Position?](https://www.textcontrol.com/blog/2007/06/22/is-there-a-table-at-the-current-mouse-position/llms.txt)
- [Apply a Boxed Frame Style to Selected Cells](https://www.textcontrol.com/blog/2007/06/12/apply-a-boxed-frame-style-to-selected-cells/llms.txt)
- [Copy All Styles from One TextControl into Another](https://www.textcontrol.com/blog/2007/05/31/copy-all-styles-from-one-textcontrol-into-another/llms.txt)
- [Show Image Anchor Positions Using GDI+](https://www.textcontrol.com/blog/2007/04/25/show-image-anchor-positions-using-gdi/llms.txt)
- [Getting the Text of a Specific Page Without the Selection Class](https://www.textcontrol.com/blog/2007/04/24/getting-the-text-of-a-specific-page-without-the-selection-class/llms.txt)
