Monday, 28 February 2011

Customizing Appearances of Individual Rows and Cells

Source : http://documentation.devexpress.com/#WindowsForms/CustomDocument758

The XtraGrid from version 3 onwards can be customized using the Appearance technology. This document demonstrates how to specify appearance settings for individual rows and cells.
The Basics of Customizing the Appearances of Rows and Cells
XtraGrid provides a number of methods that can be used to customize the appearance of rows and cells.
If these techniques don't suit your needs you can use the Appearance specific events that are more flexible.
Each of these events provides the Appearance parameter whose settings can be used to customize the background and foreground colors, gradient mode, text display options, etc.
Customizing the Appearances of Rows
In the following example the GridView.RowStyle event is handled to customize the appearance of rows which have the value "Beverages" in the Category column. These rows are painted using gradient filling. The starting and ending gradient colors are specified via the AppearanceObject.BackColor and AppearanceObject.BackColor2 properties of the event's RowStyleEventArgs.Appearance parameter.
The result is displayed below. Note that the gradients are applied to the entire rows and not to individual cells:
C#
VB
using DevExpress.XtraGrid.Views.Grid;

private void gridView1_RowStyle(object sender, 
DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
   GridView View = sender as GridView;
   if(e.RowHandle >= 0) {
      string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
      if(category == "Beverages") {
         e.Appearance.BackColor = Color.Salmon;
         e.Appearance.BackColor2 = Color.SeaShell;
      }            
   }
}


Customizing the Appearances of Cells
The following example demonstrates using the GridView.RowCellStyle event. This event is fired for each cell in a Grid View and thus it has Column and RowHandle parameters that identify the cell being painted.
In the example below the appearance of cells in the "Count" and "Unit Price" columns are customized if they reside within rows that contain the "Seafood" string in the "Category" column.
C#
VB
using DevExpress.XtraGrid.Views.Grid;
// ... 
private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) {
   GridView View = sender as GridView;
   if(e.Column.FieldName == "Count" || e.Column.FieldName == "Unit Price") {
      string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
      if(category == "Seafood") {
         e.Appearance.BackColor = Color.DeepSkyBlue;
         e.Appearance.BackColor2 = Color.LightCyan;
      }
   }
}

The following image shows the result. Note that unlike the previous example the gradient is applied to individual cells and not to entire rows.

1 comment: