Skip to main content

GridView.CustomDrawGroupRow Event

Enables you to paint group rows manually.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v23.2.dll

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

Declaration

[DXCategory("CustomDraw")]
public event RowObjectCustomDrawEventHandler CustomDrawGroupRow

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.

The event data class exposes the following methods:

Method Description
DefaultDraw() Performs default painting of an element. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, DxHtmlPainterContext, Action<DxHtmlPainterArgs>) Paints the required HTML template inside an element that raised this event. The context parameter allows you to assign an object that transfers mouse events to template elements. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, Action<DxHtmlPainterArgs>) Paints the required HTML template inside an element that raised this event. Inherited from CustomDrawEventArgs.

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";
}

Example

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; ;
        }
    };
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomDrawGroupRow 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.

See Also