FlexGrid for WinForms
Owner-Drawn Cells
Show AllShow All
Hide AllHide All

Even though the CellStyle objects offer a lot of control over the cell appearance (back and foreground colors, alignment, font, margins, borders, and so on), sometimes that is not enough. You may want to use a gradient background, or draw some custom graphics directly into a cell. In these cases, you can use the DrawMode property and the OwnerDrawCell event to gain total control over how each cell is drawn.

The DrawMode property determines whether or not the OwnerDrawCell event is fired. The event allows you to override every visual aspect of the cell. The parameters in the OwnerDrawCell event allow you to change the data that is displayed, the style used to display the data, or to take over completely and draw whatever you want into the cell.

You can change the text and image that will be shown in the cell by setting the e.Text and e.Image parameters in the event handler. You can also change the style that will be used to display the cell by setting the e.Style property.

Note that you should not modify the properties of the Style parameter because that might affect other cells. Instead, assign a new CellStyle object to the Style parameter. For example, instead of setting e.Style.ForeColor = Color.Red, assign a whole new style to the parameter using e.Style = _flex.Styles["RedStyle"].

You can also use your own drawing code to draw into the cell, and even combine your custom code with calls to the e.DrawCell method. For example, you could paint the cell background using GDI calls and then call e.DrawCell to display the cell borders and content.

Note:For an example of advanced Owner-Draw Cells, see the RtfGrid sample on ComponentOne HelpCentral.

The code below shows an example. It uses a gradient brush to paint the background of the selected cells. First, the code sets the DrawMode property, declares a LinearGradientBrush object and updates the brush whenever the grid is resized:

To write code in Visual Basic

To write code in C#

The second step is handling the OwnerDrawCell event and using the custom brush for painting the cell background. In this example, the foreground is handled by the grid itself, using the DrawCell method in the event argument:

To write code in Visual Basic

To write code in C#

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Product Support Forum |  Documentation Feedback