Formatting Cells
Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse), all customization should be reset for the rest of the cell elements. Please refer to the Fundamentals topic for more information.
RELATED VIDEOS | |
---|---|
Formatting Data In RadGridView for WinForms In this RadTip, John Kellar demonstrates how you can apply custom formatting to data within a RadGridView for Windows Forms. (Runtime: 09:14) |
Formatting data cells.
The CellFormatting event is used to access and change the styles of the data cells including the new row cells. For example, the code sample below changes the ForeColor in a particular column:
Changing the data cells font color
Cells background formatting
This is an advanced example of using CellFormatting event to highlight certain cells in aqua color based on the values of cells in the same row but different column. In the example, the values in the first column are highlighted if the value in the check box column returns true:
Formatting cells
Formatting non-data cells
The ViewCellFormatting event is fired for all cells. So if you want to format the grouping row or the header cells, you should use this event.
Change group and header cells font and removing the default filter operator text.
For example, to change the font of the header cells and the group cells use the following code:
Formatting non-data rows
Formatting cells on demand
Sometimes you may need to format the cells on a specific user action, for example, on a button click. Let's take the following scenario: you have a search box (RadTextBox) above RadGridView and a RadButton. When you click the button, RadGridView should highlight the cells that match the text typed in the RadTextBox. Here is how you can implement this:
- First, you should handle the CellFormatting event and set the back color of the cells whose text matches the text in the RadTextBox.
- The user types some text, but then you should somehow notify RadGridView that it needs to refresh itself. This is done by calling the Update method of the TableElement, passing the StateChanged argument as a parameter.
As a result of the update call, the CellFormatting event (and the other formatting events as well) will be triggered and you will get the following results: