Back to Devexpress

GridView.CustomDrawGroupRow Event

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

latest17.6 KB
Original Source

GridView.CustomDrawGroupRow Event

Allows you to paint group rows.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[DXCategory("CustomDraw")]
public event RowObjectCustomDrawEventHandler CustomDrawGroupRow
vb
<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:

PropertyDescription
AppearanceGets the painted element’s appearance settings. Inherited from CustomDrawEventArgs.
BoundsReturns a value specifying limits for the drawing area. Inherited from CustomDrawEventArgs.
CacheProvides methods to paint on drawing surfaces in GDI+ and DirectX modes. See DirectX hardware acceleration to learn more. Inherited from CustomDrawEventArgs.
GraphicsA GDI+ drawing surface. Use the CustomDrawEventArgs.Cache property instead if you enable the DirectX hardware acceleration. Inherited from CustomDrawEventArgs.
HandledGets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
InfoGets an object containing information about the painted element. Inherited from CustomDrawObjectEventArgs.
PainterGets the painter object that provides the default element painting mechanism. Inherited from CustomDrawObjectEventArgs.
RowHandleGets the handle of the row whose corresponding element is being painted.

The event data class exposes the following methods:

MethodDescription
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 group row is identified by the e.RowHandle event parameter. Use the GridView.GetRowLevel method to identify the group row’s nesting level.

Type cast the e.Info event parameter to the DevExpress.XtraGrid.Views.Grid.ViewInfo.GridGroupRowInfo type to information specific to group rows. Use the e.Info.GroupText property to specify the text displayed within the group row.

The following example handles the CustomDrawGroupRow event to hide the data field name from group rows:

csharp
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Collections.Generic;

namespace DXApplication {
    public partial class Form1 : XtraForm {
        public Form1() {
            InitializeComponent();
            gridControl1.DataSource = new List<DataItem>() {
                new DataItem(){ FirstName = "Bob", LastName = "Smith" },
                new DataItem(){ FirstName = "Ann", LastName = "Miller" },
                new DataItem(){ FirstName = "Sandra", LastName = "Moore" },
                new DataItem(){ FirstName = "Samuel", LastName = "Brown" },
            };
            Load += Form1_Load;
            gridView1.CustomDrawGroupRow += gridView1_CustomDrawGroupRow;
            gridView1.OptionsView.ShowGroupedColumns = true;
        }
        void Form1_Load(object sender, System.EventArgs e)
        {
            gridView1.Columns["FirstName"].GroupIndex = 0;
            gridView1.Columns["FirstName"].GroupInterval = ColumnGroupInterval.Alphabetical;
        }
        void gridView1_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo info = e.Info as GridGroupRowInfo;
            if (info.Column.FieldName == "FirstName")
                info.GroupText = info.GroupValueText;
        }
    }
    public class DataItem {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
vb
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports System.Collections.Generic

Namespace DXApplication
    Partial Public Class Form1
        Inherits XtraForm

        Public Sub New()
            InitializeComponent()
            gridControl1.DataSource = New List(Of DataItem)() From {
                New DataItem() With {
                    .FirstName = "Bob",
                    .LastName = "Smith"
                },
                New DataItem() With {
                    .FirstName = "Ann",
                    .LastName = "Miller"
                },
                New DataItem() With {
                    .FirstName = "Sandra",
                    .LastName = "Moore"
                },
                New DataItem() With {
                    .FirstName = "Samuel",
                    .LastName = "Brown"
                }
            }
            AddHandler Load, AddressOf Form1_Load
            AddHandler gridView1.CustomDrawGroupRow, AddressOf gridView1_CustomDrawGroupRow
            gridView1.OptionsView.ShowGroupedColumns = True
        End Sub
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            gridView1.Columns("FirstName").GroupIndex = 0
            gridView1.Columns("FirstName").GroupInterval = ColumnGroupInterval.Alphabetical
        End Sub
        Private Sub gridView1_CustomDrawGroupRow(ByVal sender As Object, ByVal e As RowObjectCustomDrawEventArgs)
            Dim info As GridGroupRowInfo = TryCast(e.Info, GridGroupRowInfo)
            If info.Column.FieldName = "FirstName" Then
                info.GroupText = info.GroupValueText
            End If

        End Sub
    End Class
    Public Class DataItem
        Public Property FirstName() As String
        Public Property LastName() As String
    End Class
End Namespace

The GridView.GroupLevelStyle event allows you to paint 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 uses HTML tags to format text within group rows. The example handles the CustomDrawGroupRow event. In the example, when data is grouped by the “Quantity” column, group values are painted in different colors using the <color> tag. New display text for group rows is supplied via the e.Info.GroupText property.

csharp
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";
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

gridView1.OptionsView.AllowHtmlDrawGroups = True

Private Sub GridView1_CustomDrawGroupRow(sender As System.Object, _
    e As RowObjectCustomDrawEventArgs) Handles GridView1.CustomDrawGroupRow
    Dim view As GridView = TryCast(sender, GridView)
    Dim info As GridGroupRowInfo = TryCast(e.Info, GridGroupRowInfo)
    If info.Column.Caption = "Quantity" Then
        Dim quantity As Integer = Convert.ToInt32(view.GetGroupRowValue(e.RowHandle, info.Column))
        Dim colorName As String = getColorName(quantity)
        info.GroupText = info.Column.Caption & ": <color=" & colorName & ">" & info.GroupValueText & "</color> "
        info.GroupText &= "<color=LightSteelBlue>" & view.GetGroupSummaryText(e.RowHandle) & "</color> "
    End If
End Sub

Private Function getColorName(ByVal value As Integer) As String
    If value < 20 Then
        Return "MediumOrchid"
    End If
    If value >= 80 Then
        Return "OrangeRed"
    End If
    Return "Blue"
End Function

Example

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

csharp
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; ;
        }
    };
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports System.Drawing.Drawing2D

Private Sub Form3_Load1(ByVal sender As Object, ByVal e As EventArgs)
    CustomDrawGroupRow(gridControl1, gridView1)
End Sub

Public Shared Sub CustomDrawGroupRow(ByVal gridControl As GridControl, ByVal gridView As GridView)
    gridView.Columns("Category_Name").Group()

    ' Handle this event to paint group rows manually
    AddHandler gridView.CustomDrawGroupRow, Sub(s, e)
        If e.RowHandle Mod 2 = 0 Then
            e.Appearance.BackColor = Color.BlanchedAlmond
            e.Appearance.ForeColor = Color.DimGray

        End If
    End Sub
End Sub

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.

winforms-grid-enable-editing-in-group-row-to-change-cell-values/CS/WindowsApplication3/GroupEditProvider.cs#L73

csharp
{
    view.CustomDrawGroupRow += OnCustomDrawGroupRow;
    view.MouseDown += OnMouseDown;

winforms-grid-multiple-row-selection-web-style-checkboxes/CS/E1271/CheckMarkSelection.cs#L131

csharp
view.CustomDrawColumnHeader += new ColumnHeaderCustomDrawEventHandler(View_CustomDrawColumnHeader);
view.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(View_CustomDrawGroupRow);
view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);

winforms-grid-enable-editing-in-group-row-to-change-cell-values/VB/WindowsApplication3/GroupEditProvider.vb#L76

vb
Public Sub EnableGroupEditing()
    AddHandler view.CustomDrawGroupRow, AddressOf OnCustomDrawGroupRow
    AddHandler view.MouseDown, AddressOf OnMouseDown

winforms-grid-multiple-row-selection-web-style-checkboxes/VB/E1271/CheckMarkSelection.vb#L159

vb
AddHandler view.CustomDrawColumnHeader, New ColumnHeaderCustomDrawEventHandler(AddressOf View_CustomDrawColumnHeader)
AddHandler view.CustomDrawGroupRow, New RowObjectCustomDrawEventHandler(AddressOf View_CustomDrawGroupRow)
AddHandler view.CustomUnboundColumnData, New CustomColumnDataEventHandler(AddressOf view_CustomUnboundColumnData)

See Also

GroupLevelStyle

Custom Painting Basics

Custom Painting Scenarios

Elements that Can Be Custom Painted

Manually Invalidating Controls

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace