Back to Devexpress

How to: Dynamically Customize Cell Appearance

windowsforms-3038-controls-and-libraries-data-grid-examples-painting-how-to-dynamically-customize-cell-appearance.md

latest1.5 KB
Original Source

How to: Dynamically Customize Cell Appearance

  • Nov 13, 2018

The following code demonstrates how to customize the appearance of individual grid cells using the GridView.RowCellStyle event handler. The grid cells in this example are colored in a staggered manner. The following image demonstrates the result:

csharp
using DevExpress.XtraGrid.Views.Grid;

private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) {
   GridView view = sender as GridView;
   if(view == null) return;
   if(e.RowHandle != view.FocusedRowHandle && 
      ((e.RowHandle % 2 == 0 && e.Column.VisibleIndex % 2 == 1) || 
      (e.Column.VisibleIndex % 2 == 0 && e.RowHandle % 2 == 1)))    
      e.Appearance.BackColor = Color.NavajoWhite;
}
vb
Imports DevExpress.XtraGrid.Views.Grid

Private Sub gridView1_RowCellStyle(ByVal sender As Object, _
  ByVal e As RowCellStyleEventArgs) Handles gridView1.RowCellStyle
    Dim view As GridView = sender
    If view Is Nothing Then
        Return
    End If
    If e.RowHandle <> view.FocusedRowHandle And _
        ((e.RowHandle Mod 2 = 0 And e.Column.VisibleIndex Mod 2 = 1) Or _
        (e.Column.VisibleIndex Mod 2 = 0 And e.RowHandle Mod 2 = 1)) Then _
            e.Appearance.BackColor = Color.NavajoWhite
End Sub