GridView.CustomDrawGroupRow Event

Enables you to paint group rows manually.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v20.1.dll

Declaration

[DXCategory("CustomDraw")]
public event RowObjectCustomDrawEventHandler CustomDrawGroupRow
<DXCategory("CustomDraw")>
Public Event CustomDrawGroupRow As RowObjectCustomDrawEventHandler

Event Data

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

Property Description
Appearance Gets the painted element's appearance settings. Inherited from CustomDrawEventArgs.
Bounds Returns a value specifying limits for the drawing area. Inherited from CustomDrawEventArgs.
Cache Provides methods to paint on drawing surfaces in GDI+ and DirectX modes. See DirectX hardware acceleration to learn more. Inherited from CustomDrawEventArgs.
Graphics A GDI+ drawing surface. Use the CustomDrawEventArgs.Cache property instead if you enable the DirectX hardware acceleration. Inherited from CustomDrawEventArgs.
Handled Gets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
Info Gets an object containing information about the painted element. Inherited from CustomDrawObjectEventArgs.
Painter Gets the painter object that provides the default element painting mechanism. Inherited from CustomDrawObjectEventArgs.
RowHandle Gets the handle of the row whose corresponding element is being painted.

Remarks

The CustomDrawGroupRow event is raised each time a group row needs to be repainted. The row is identified by the RowObjectCustomDrawEventArgs.RowHandle parameter. The row's nesting level can be determined using the View's GridView.GetRowLevel method. This method's return value also identifies the grouping column corresponding to the group row.

The event's CustomDrawObjectEventArgs.Info parameter of the DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo type provides information specific to the group row display. For instance, the GroupText property accessible via the Info parameter specifies the group row's display text (cast this parameter to the GridGroupRowInfo type beforehand).

See the Custom Painting Basics and Custom Painting Scenarios topics for information on using custom draw events.

The GridView.GroupLevelStyle event allows you to customize group row indents.

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.

Online Video

WinForms Grid: Expand Rows when Grouping

Example

The following example shows how to use HTML tags to format text in group rows via the GridView.CustomDrawGroupRow event. In the example, when data is grouped by the Quantity column, the column's group values are painted in different colors using the <color> tag.

GroupRows-HTML-tags-via-CustomDraw

New display text for group rows is supplied via the GroupText property of the event's e.Info parameter.

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

gridView1.OptionsView.AllowHtmlDrawGroups = true;

private void gridView1_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e) {
    GridView view = sender as GridView;
    GridGroupRowInfo info = e.Info as GridGroupRowInfo;
    if (info.Column.Caption == "Quantity") {
        int quantity = Convert.ToInt32(view.GetGroupRowValue(e.RowHandle, info.Column));
        string colorName = getColorName(quantity);
        info.GroupText = info.Column.Caption + ": <color=" + colorName + ">" + info.GroupValueText + "</color> ";
        info.GroupText += "<color=LightSteelBlue>" + view.GetGroupSummaryText(e.RowHandle) + "</color> ";
    }
}

string getColorName(int value) {
    if (value < 20) return "MediumOrchid";
    if (value >= 80) return "OrangeRed";
    return "Blue";
}

Examples

The code below applies grouping by the "Category_Name" column and changes the background and foreground colors for even group rows.

GridControl - CustomDrawGroupRows

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Drawing.Drawing2D;

private void Form3_Load1(object sender, EventArgs e) {
    CustomDrawGroupRow(gridControl1, gridView1);
}

public static void CustomDrawGroupRow(GridControl gridControl, GridView gridView) {
    gridView.Columns["Category_Name"].Group();

    // Handle this event to paint group rows manually
    gridView.CustomDrawGroupRow += (s, e) => {
        if (e.RowHandle % 2 == 0) {
            e.Appearance.BackColor = Color.BlanchedAlmond;
            e.Appearance.ForeColor = Color.DimGray; ;
        }
    };
}
See Also