To control how content of the cells is formatted and displayed, set the Format property of Row or Column object to format string similar to the ones that are used with String.Format property in the .NET framework. For instance, the sample code below formats the third column as Date and fourth column as Currency.
FlexGrid also provides design time option to set the Format property through the Format String dialog. You can access the Format String dialog by clicking Ellipsis button against the Format String field in Column Tasks menu. Or, you can also use the Format property in C1FlexGrid Column Editor.
Note that Format String dialog is specific to column and changes the Format property of the selected column only.
FlexGrid provides CellStyle objects to handle the appearance aspects of cells such as alignment, font, colors, borders, and so on. The grid has Styles property that holds the collection of styles used to format the grid. This collection has some built-in members that define the appearance of grid elements, such as fixed and scrollable cells, selection, focus cell, and so on. You can change these styles to modify the appearance of grid.
Although, changing the built-in styles is the simplest way to change appearance of the grid, you can also create your own custom styles and assign them to cells, rows, or columns.
Use the code below to change appearance of the WinForms FlexGrid cells.
To format a cell according to its content, you need to create a new style and apply it to the cells satisfying a particular condition using SetCellStyle() method. For instance, to highlight the values greater than a specified value, you can format the cells containing them using different style.
Following code shows how to apply conditional formatting on WinForms FlexGrid cells.
The above sections explain how you can customize FlexGrid cells to change the appearance of grid using CellStyle objects. However, for further customization of grid cells such as rendering gradient background, custom graphics etc., you can use DrawMode property and OwnerDrawCell event of the C1FlexGrid class.
The DrawMode property determines whether or not the OwnerDrawCell event is fired. The event allows you to override every visual aspect of the cell. You can change the text and image to be displayed in the cell by setting the e.Text and e.Image parameters in the event handler. You can also change the style that is used to display the cell by setting the e.Style property.
Note that you should not modify properties of the Style parameter as it 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 = c1FlexGrid1.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 can paint the cell background using GDI calls and then call e.DrawCell to display the cell borders and content.
In the example below, WinForms FlexGrid uses a gradient brush to paint the background of the selected cell range. First, the sample code sets the DrawMode property to OwnerDraw, and then declares a LinearGradientBrush object.