GridView.CustomDrawGroupRow Event
Allows you to paint group rows.
Namespace: DevExpress.XtraGrid.Views.Grid
Assembly: DevExpress.XtraGrid.v24.2.dll
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 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:
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; }
}
}
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.
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.
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; ;
}
};
}
Related GitHub Examples
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.