Back to Devexpress

GridView.RowCellStyle Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-ec44b848.md

latest10.7 KB
Original Source

GridView.RowCellStyle Event

Fires for every visible Grid cell before this cell is shown. Allows you to modify Appearance settings for this cell.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Appearance")]
public event RowCellStyleEventHandler RowCellStyle
vb
<DXCategory("Appearance")>
Public Event RowCellStyle As RowCellStyleEventHandler

Event Data

The RowCellStyle event's data class is RowCellStyleEventArgs. The following properties provide information specific to this event:

PropertyDescription
AppearanceGets the appearance settings used to paint the data cell currently being processed.
CellValueReturns the currently processed cell value. Inherited from CustomRowCellEventArgs.
ColumnGets the column to which the currently processed cell corresponds. Inherited from CustomRowCellEventArgs.
RowHandleGets the handle of the row that contains the processed cell. Inherited from CustomRowCellEventArgs.

The event data class exposes the following methods:

MethodDescription
CombineAppearance(AppearanceObject)Copies the activated settings of the appearance object passed as the parameter.

Remarks

Use the event’s e.RowHandle and e.Column parameters to identify the currently processed cell, then use the e.Appearance parameter to customize appearance settings.

Use the RefreshRowCell method to forcibly raise the RowCellStyle event when needed.

Note

The grid control fires the RowCellStyle event too frequently. We recommend that you do not implement complex logic in this event handler to avoid performance issues.

[Run Demo](dxdemo://Win/XtraGrid/MainDemo/CodeExamples/Appearance_customization.Appearance_of_column_cells_(dynamically)

csharp
gridView.RowCellStyle += (sender, e) => {
        GridView view = sender as GridView;
        bool _mark = (bool)view.GetRowCellValue(e.RowHandle, "Mark");
        if(e.Column.FieldName == "Name") {
            e.Appearance.BackColor = _mark ? Color.LightGreen : Color.LightSalmon;
            e.Appearance.TextOptions.HAlignment = _mark ? HorzAlignment.Far : HorzAlignment.Near;
        }
        if(e.Column.FieldName == "Length") {
            double _length = (double)e.CellValue;
            if(_length > 25)
                e.Appearance.ForeColor = Color.Red;
        }
    };
vb
AddHandler gridView.RowCellStyle, Sub(sender, e)
        Dim view As GridView = TryCast(sender, GridView)
        Dim _mark As Boolean = CBool(view.GetRowCellValue(e.RowHandle, "Mark"))
        If e.Column.FieldName = "Name" Then
            e.Appearance.BackColor = If(_mark, Color.LightGreen, Color.LightSalmon)
            e.Appearance.TextOptions.HAlignment = If(_mark, HorzAlignment.Far, HorzAlignment.Near)
        End If
        If e.Column.FieldName = "Length" Then
            Dim _length As Double = CDbl(e.CellValue)
            If _length > 25 Then
                e.Appearance.ForeColor = Color.Red
            End If
        End If
    End Sub

To specify appearance settings of entire rows instead of individual cells, handle the GridView.RowStyle event.

Tip

Data Grid does not display cell images assigned to the Appearance.Image properties in the RowCellStyle and GridView.RowStyle events. See the “Cell Icons” section of the Cells topic to learn how to display icons within Grid cells.

Important

Do not change cell values, modify the control’s layout, or change the control’s object model in the events used for custom control painting. Actions that update the layout can cause the control to malfunction.

Please refer to the Appearance and Conditional Formatting topic for additional information.

Online Video

WinForms Grid: Custom Drawing.

Example

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the RowCellStyle event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-highlight-modified-cells/CS/Form1.cs#L15

csharp
gridControl1.DataSource = GetData(10);
gridView1.RowCellStyle += gridView1_RowCellStyle;
gridView1.CellValueChanged += gridView1_CellValueChanged;

winforms-grid-customize-cell-color/CS/CellColorHelper.cs#L24

csharp
_View = view;
    _View.RowCellStyle += _View_RowCellStyle;
}

winforms-grid-update-row-style-on-cell-value-change/CS/Form1.cs#L49

csharp
riTrackBar.EditValueChanged += OnEditValueChanged;
    gridView1.RowCellStyle += new RowCellStyleEventHandler(gridView1_RowCellStyle);
}

winforms-grid-highlight-modified-cells/VB/Form1.vb#L17

vb
gridControl1.DataSource = GetData(10)
AddHandler gridView1.RowCellStyle, AddressOf gridView1_RowCellStyle
AddHandler gridView1.CellValueChanged, AddressOf gridView1_CellValueChanged

winforms-grid-customize-cell-color/VB/CellColorHelper.vb#L21

vb
_View = view
    AddHandler _View.RowCellStyle, AddressOf _View_RowCellStyle
End Sub

winforms-grid-update-row-style-on-cell-value-change/VB/Form1.vb#L48

vb
AddHandler riTrackBar.EditValueChanged, AddressOf OnEditValueChanged
    AddHandler gridView1.RowCellStyle, New RowCellStyleEventHandler(AddressOf gridView1_RowCellStyle)
End Sub

See Also

CalcRowHeight

CustomDrawCell

RowStyle

RefreshRow

RefreshRowCell

AppearanceCell

Appearance and Conditional Formatting

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace