KB Article A1498
Visible to All Users

Is custom drawing ignored when printing or exporting?

Description:
I'm handling the CustomDraw~ events of the grid. But I noticed that my customizations do not appear when a component is printed via the XtraPrinting Library. How do I get the customizations to show in the printed version?

Answer:
Applies to: XtraGrid, XtraTreeList, XtraVerticalGrid
Yes, custom drawing is not taken into account during printing and exporting. There are several solutions depending on what you are using the CustomDraw~ events for.

- If you use them to customize the displayed text, please try to use column formatting or customize the grid's underlying data.
- If the CustomDrawCell event is utilized to change a cell's style, please use the GridView.RowCellStyle event instead (the NodeCellStyle event of the TreeList).
- When the data-aware export engine is used, customizations made in the RowCellStyle event handler are not taken into account. If you wish to customize the appearance settings of grid cells, create a new XlsxExportOptionsEx object and handle the CustomizeCell event. Then, pass this object to the GridView.ExportToXlsx method.

Here is the code:

C#
private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) { ColorizeCell(e.Column.FieldName, e.RowHandle, e.Appearance); } private void ColorizeCell(string fieldName, int rowHandle, object appearanceObject) { if (fieldName == "Text" && rowHandle >= 0 && rowHandle % 2 != 0) { AppearanceObject app = appearanceObject as AppearanceObject; if (app != null) app.BackColor = Color.Indigo; else { XlFormattingObject obj = appearanceObject as XlFormattingObject; if (obj != null) obj.BackColor = Color.Indigo; } } } private void simpleButton1_Click(object sender, EventArgs e) { string filePath = "Excrl.xlsx"; XlsxExportOptionsEx opt = new XlsxExportOptionsEx(); opt.CustomizeCell += opt_CustomizeCell; gridView1.ExportToXlsx(filePath, opt); Process.Start(filePath); } void opt_CustomizeCell(DevExpress.Export.CustomizeCellEventArgs e) { ColorizeCell(e.ColumnFieldName, e.RowHandle, e.Formatting); e.Handled = true; }
Visual Basic
Private Sub gridView1_RowCellStyle(ByVal sender As Object, ByVal e As RowCellStyleEventArgs) ColorizeCell(e.Column.FieldName, e.RowHandle, e.Appearance) End Sub Private Sub ColorizeCell(ByVal fieldName As String, ByVal rowHandle As Integer, ByVal appearanceObject As Object) If fieldName = "Text" AndAlso rowHandle >= 0 AndAlso rowHandle Mod 2 <> 0 Then Dim app As AppearanceObject = TryCast(appearanceObject, AppearanceObject) If app IsNot Nothing Then app.BackColor = Color.Indigo Else Dim obj As XlFormattingObject = TryCast(appearanceObject, XlFormattingObject) If obj IsNot Nothing Then obj.BackColor = Color.Indigo End If End If End If End Sub Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim filePath As String = "Excrl.xlsx" Dim opt As New XlsxExportOptionsEx() AddHandler opt.CustomizeCell, AddressOf opt_CustomizeCell gridView1.ExportToXlsx(filePath, opt) Process.Start(filePath) End Sub Private Sub opt_CustomizeCell(ByVal e As DevExpress.Export.CustomizeCellEventArgs) ColorizeCell(e.ColumnFieldName, e.RowHandle, e.Formatting) e.Handled = True End Sub

- If you need to completely change a cell's appearance, please create a custom editor for the grid. In this case, you will need to override the RepositoryItem.GetBrick method. For code samples code please see our implementation in the DevExpress.XtraEditors source files. When handling the CustomDraw~ events for painting columns, footers, and indicators, the only way to print the customized view is to re-implement grid (TreeList) painting in a descendant class instead of handling the CustomDraw~ events: How to customize the GridControl's print output

See Also:
How to customize the Look-And-Feel of my grid cells
How to register a custom editor for use in the XtraGrid

Comments (2)
D D
dormakaba Schweiz AG 10 years ago

    Nick you are superb!!!
    A255 How to customize the Look-And-Feel of my grid cells
    is EXACTLY what I was looking for.
    I also can now get rid of CustomDraw event because RowCellStyle make exactly what I want but with less code lines :-D
    Thanks!!

    DevExpress Support Team 10 years ago

      Hello,

      Thank you for informing us of your results. We greatly appreciate your opinion and are always happy to assist you.

      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.