Back to Devexpress

TcxGridTableView.OnCustomDrawGroupCell Event

vcl-cxgridtableview-dot-tcxgridtableview-9eef80cb.md

latest6.2 KB
Original Source

TcxGridTableView.OnCustomDrawGroupCell Event

Allows you to complement or override built-in group row draw routines.

Declaration

delphi
property OnCustomDrawGroupCell: TcxGridTableCellCustomDrawEvent read; write;

Remarks

You can handle the OnCustomDrawGroupCell event to change the appearance of individual group rows depending on specific conditions in your application.

Event Occurrence

The OnCustomDrawGroupCell event occurs every time the grid View is about to draw a group row cell.

Since the OnCustomDrawGroupCell event may occur often, the assigned handler must not include time-consuming calculations. Otherwise, the resource-intensive custom draw routine may render the application UI slow and unresponsive.

Important

If you need to rely on data-dependent conditions in an OnCustomDrawGroupCell event handler, we strongly recommend that you use only cached or pre-calculated values instead of complex calculations at the data controller or dataset level.

Event Parameters

The following parameters are available within an OnCustomDrawGroupCell event handler:

SenderProvides access to the grid Table View that raised the group cell draw event.ACanvasProvides access to the processed cell’s canvas. You can call canvas draw routines accessible through this parameter to display custom group cell content.ViewInfoReturns information required to draw the processed group cell. For example, you can use the ViewInfo.Bounds field to identify the boundaries of the cell’s canvas.ADoneSpecifies if built-in group cell draw routines are disabled. Assign True to this parameter within an OnCustomDrawGroupCell event handler to implement the draw routine for the processed cell from scratch.

Refer to the TcxGridTableCellCustomDrawEvent procedural type description for detailed information on all available options.

Code Example: Change Group Row Appearance Based on Content

Built-in draw routines display group rows according to active look & feel settings and other grid View options that affect group row appearance and layout:

The code example in this topic section demonstrates an OnCustomDrawGroupCell event handler that implements a custom draw routine for group rows. This handler modifies font attributes and changes row background color depending on content:

delphi
uses
  cxGrid, // Declares the TcxGrid control
  cxGridTableView, // Declares the TcxGridTableView class
  StrUtils; // Declares the ContainsText function
// ...

procedure TMyForm.cxGrid1TableView1CustomDrawGroupCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableCellViewInfo; var ADone: Boolean);
var
  AString: string;
  ARect: TRect;
begin
  AString := AViewInfo.Text;
  ARect := AViewInfo.Bounds;
  ARect.Left := ARect.Left + AViewInfo.ScaleFactor.Apply(15);
  ARect.Top := ARect.Top + AViewInfo.ScaleFactor.Apply(5);
  if ContainsText(AViewInfo.Text, 'City') then
  begin
    ACanvas.Font.Style := [fsBold, fsItalic];
    ACanvas.FillRect(AViewInfo.Bounds, clWebLightGreen);
  end
  else
  begin
    ACanvas.Font.Color := clWhite;
    ACanvas.Font.Style := [fsBold];
    ACanvas.FillRect(AViewInfo.Bounds, clWebLightCoral);
  end;
  ACanvas.DrawTexT(AString, ARect, 0, True);
  ADone := True;
end;
cpp
#include "cxGrid.hpp" // Declares the TcxGrid control
#include "cxGridTableView.hpp" // Declares the TcxGridTableView class
#include "StrUtils.hpp" // Declares the ContainsText function

// Add the following linker directives to the corresponding CPP source file:
#pragma link "cxGrid" // Required to use cxGrid.hpp declarations
#pragma link "cxGridTableView" // Required to use cxGridTableView declarations
// ...

void __fastcall TMyForm::cxGrid1TableView1CustomDrawGroupCell(
  TcxCustomGridTableView *Sender, TcxCanvas *ACanvas,
  TcxGridTableCellViewInfo *AViewInfo, bool &ADone)
{
  UnicodeString AString = AViewInfo->Text;
  TRect ARect = AViewInfo->Bounds;
  ARect.Left = ARect.Left + AViewInfo->ScaleFactor->Apply(15);
  ARect.Top = ARect.Top + AViewInfo->ScaleFactor->Apply(5);
  if(ContainsText(AViewInfo->Text, "City"))
  {
    ACanvas->Font->Style = TFontStyles() << fsBold << fsItalic;
    ACanvas->FillRect(AViewInfo->Bounds, clWebLightGreen);
  }
  else
  {
    ACanvas->Font->Color = clWhite;
    ACanvas->Font->Style = TFontStyles() << fsBold;
    ACanvas->FillRect(AViewInfo->Bounds, clWebLightCoral);
  }
  ACanvas->DrawTexT(AString, ARect, 0, true);
  ADone = true;
}

Other Custom Draw Events

OnCustomDrawCellAllows you to override or complement built-in cell draw routines.OnCustomDrawColumnHeaderOccurs every time when a column header is about to be drawn.OnCustomDrawFooterCellOccurs every time a footer or group footer cell is about to be drawn.OnCustomDrawGroupSummaryCellOccurs every time a group summary is about to be drawn in a group row.OnCustomDrawIndicatorCellOccurs when painting an indicator cell within a Table View.OnCustomDrawPartBackgroundAllows you to custom draw a View element background. See Also

TcxGridTableView Class

TcxGridTableView Members

cxGridTableView Unit