Custom Painting Basics
- 4 minutes to read
Like other DevExpress .NET WinForms controls, the GridControl supports the Appearances mechanism, to customize the look and feel of its elements. It allows you to modify attributes such as background and foreground colors, background images, font settings, etc. If you need more control over element rendering, consider using the custom draw events provided by the GridControl, for each visual element it contains.
Custom painting and alpha blending features are not applied to the printed and exported versions of the Grid Control.
Learn how to custom draw row cells and column headers using dedicated events.
- WinForms Grid: How to Custom Draw Footer Cells
- WinForms Grid: Custom Row Indicators
- WinForms Grid: Expand Rows when Grouping
- WinForms Grid: Draw a Border Cell
Custom Painting Basics
Custom draw events allow you to do the following.
Paint control elements manually.
Invoke the default painting mechanism of an element.
It is possible to modify specific view information (e.g., display text) before invoking the default painting mechanism. The default painting method will automatically apply the changes made.
The image below shows a custom painted Grid View.
Custom draw events are raised before grid elements are painted. Each custom draw event handler receives a CustomDrawEventArgs descendant as a parameter. This object's properties specify the bounds of an element, the graphic surface, the appearance settings and various view information related to the painted element.
Custom draw event handlers provide the CustomDrawEventArgs.DefaultDraw method, which allows you to invoke the default painting mechanism of an element during event execution. There is also another implicit way to invoke the default painting mechanism. If the event's CustomDrawEventArgs.Handled parameter is set to false (the default value), the default painting mechanism will automatically be invoked after your custom draw event handler is completed. Note that the default painting mechanism overrides all rendering you may have performed before. The DefaultDraw method automatically sets the Handled parameter to true, to prevent the default painting mechanism from being invoked again after event execution. However, if you paint an element manually and do not use the DefaultDraw method, you should manually set the Handled parameter to true.
Some event handlers provide the Info parameter, which contains view settings specific to the painted element (display text, bounds, etc.). For instance, you can modify the Info parameter as required, and then invoke the default painting mechanism, which will render the element as specified by the Info parameter. The Info parameter is available when the following elements are painted: filter panel, band and column headers, view footer, group footer, group rows, preview section, row indicator and footer cells.
Some custom draw events provide properties that specify an element's display text. You can modify these properties and then invoke the default painting mechanism. For instance, the GridView.CustomDrawCell event provides the DisplayText property, and the CardView.CustomDrawCardCaption event provides the CardCaption property, which can be modified before the default rendering.
See the following documents for information on custom drawing approaches and a list of elements that can be custom painted.
In certain paint themes (e.g., skins and Office2003), you cannot change the background of some elements. Changing the background of these elements via the Appearance parameter of custom draw events is also not supported.