A newer version of this page is available. Switch to the current version.

Customizing Appearances of Individual Cells

  • 3 minutes to read

You can use the appearance mechanism to customize the appearance settings of data cells, cells located within a focused record and the focused cell. In addition you can customize the appearance of a particular editor row or multi-editor row to change the appearance of the cells residing within it. However these techniques don’t allow you to provide custom appearance settings for individual cells. This topic will explain how to do this.

The Basics of Assigning Styles to Individual Cells

To change the appearance settings of individual cells, handle the VGridControlBase.RecordCellStyle event. This event fires for each cell before it’s painted. The event’s Row, RecordIndex and CellIndex parameters specify the cell currently being processed. The first parameter specifies the row in which the cell resides. If an editor row’s cell is currently being processed, you will only need the RecordIndex parameter to identify the cell, since such rows display only a single cell within each record. If the cell’s owning row is a multi-editor row, you will also need the CellIndex parameter to identify the cell. Please refer to the Cells Overview topic for details on how to identify cells.

To customize the appearance of cells use the style properties provided by the event’s Appearance parameter.

The VGridControlBase.RecordCellStyle event gives you the ability to implement conditional cell formatting. This means that you can customize the styles of cells that satisfy specific conditions. Moreover, this method of customizing styles can be used to change the appearance settings of individual records. Assume that each record displays personal information and there is a Boolean row whose values specify if a particular person is an employee. In this case, you can display records with employees in a different style than other records. So there is no need to display a row containing Boolean values to distinguish between records. This lets you save screen space and makes your control more representative.

Customizing the Appearance of Records - A Sample

The sample in this section will handle the VGridControlBase.RecordCellStyle event to specify different appearance settings for the records whose Price field value is greater than $100,000.


using DevExpress.XtraVerticalGrid;
using DevExpress.Utils;
// ...
private void vGridControl1_RecordCellStyle(object sender, DevExpress.XtraVerticalGrid.Events.GetCustomRowCellStyleEventArgs e) {
   VGridControl vGrid = sender as VGridControl;
   int price = Convert.ToInt32(vGrid.GetCellValue(vGrid.Rows[1], e.RecordIndex));
   if (price < 100000) return;
   e.Appearance.BackColor = Color.AntiqueWhite;
   e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
   e.Appearance.ForeColor = Color.SaddleBrown;
} 

The image below shows the result of such event handling.

Styles - IndCells - Rows

Customizing the Appearance of Cells - A Sample

This section’s sample will handle the VGridControlBase.RecordCellStyle event to customize the appearance of individual cells. It will paint the cells of a multi-editor row using different appearance settings. The image below shows the cells that are going to be painted.

Styles - IndCells - CellsBefore


private void vGridControl1_RecordCellStyle(object sender, DevExpress.XtraVerticalGrid.Events.GetCustomRowCellStyleEventArgs e) {
   if (e.Row != rowMPG) return;
   if (e.CellIndex == 0)
      e.Appearance.BackColor = Color.Gold;
   else
      e.Appearance.BackColor = Color.LightSkyBlue;
}

The following image shows the result.

Styles - IndCells - CellsAfter