Back to Devexpress

GridView.BeforePrintRow Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-1836b177.md

latest7.7 KB
Original Source

GridView.BeforePrintRow Event

Fires before printing/exporting each individual row, and allows you to add custom information to the printout/export output, and prevent a row from being printed/exported.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[DXCategory("Printing")]
public event BeforePrintRowEventHandler BeforePrintRow
vb
<DXCategory("Printing")>
Public Event BeforePrintRow As BeforePrintRowEventHandler

Event Data

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

PropertyDescription
BrickGraphicsGets a BrickGraphics object that implements report drawing functions. Inherited from PrintRowEventArgs.
CancelGets or sets whether the current row must not be printed/exported.
GraphicsGets a GDI+ grid’s drawing surface. Inherited from PrintRowEventArgs.
HasFooterGets whether the row contains a footer. Inherited from PrintRowEventArgs.
LevelGets the group level of the printed row . Inherited from PrintRowEventArgs.
PSGets the PrintingSystem object that provides methods to create bricks in the printout/export output. Inherited from PrintRowEventArgs.
RowHandleGets the handle of the printed row. Inherited from PrintRowEventArgs.
XGets or sets the X coordinate, in pixels, where the next row will be drawn in the printout/export output. Inherited from PrintRowEventArgs.
YGets or sets the Y coordinate, in pixels, where the next row will be drawn in the printout/export output. Inherited from PrintRowEventArgs.

Remarks

When printing and exporting data, the BeforePrintRow and GridView.AfterPrintRow events fire, and these allow you to perform printout/export output customizations.

Using the BeforePrintRow event is equivalent to the GridView.AfterPrintRow event. See this link, for more information, and an example.

Example

The following example shows how to add custom information to the printout/export output when the grid control is printed/exported.

In the example, the GridView.BeforePrintRow event is handled to add a custom footer after every 5 grid rows have been printed. After every 10 rows, a page break is inserted:

Note

To compile this example, ensure that the XtraPrinting library is added to the References section of your project.

csharp
using DevExpress.XtraPrinting;

private void gridView1_BeforePrintRow(object sender, 
DevExpress.XtraGrid.Views.Printing.PrintRowEventArgs e) {
    // Insert a custom footer.
    if((e.RowHandle + 1) % 5 == 0) {
        // Create a text brick and customize its appearance settings.
        TextBrick tb = new TextBrick();
        tb.Text = "Custom footer";
        tb.Font = new DXFont(tb.Font, DXFontStyle.Bold);
        tb.HorzAlignment = HorzAlignment.Near;
        tb.Padding = new PaddingInfo(5, 0, 0, 0);
        tb.BackColor = Color.LightGray;
        tb.ForeColor = Color.Black;
        // Get the client page width.
        float textBrickHeight = e.BrickGraphics.MeasureString(tb.Text, tb.Font).Height + 4;
        // Calculate a rectangle for the brick and draw the brick.
        RectangleF textBrickRect =
            new RectangleF(0, e.Y, e.BrickGraphics.ClientPageSize.Width, textBrickHeight);
        e.BrickGraphics.DrawBrick(tb, textBrickRect);
        // Adjust the current Y position to print the following row below the brick.
        e.Y += (int)textBrickHeight;
    }
    // Add a page break.
    if((e.RowHandle + 1) % 10 == 0) {
        e.PS.InsertPageBreak(e.Y);
    }

// Show a print preview
private void simpleButton1_Click(object sender, EventArgs e) {
    gridControl1.ShowPrintPreview();
}
vb
Private Sub GridView1_BeforePrintRow(ByVal sender As System.Object, _
ByVal e As DevExpress.XtraGrid.Views.Printing.PrintRowEventArgs) Handles GridView1.BeforePrintRow
    ' Insert a custom footer.
    If (e.RowHandle + 1) Mod 5 = 0 Then
        ' Create a text brick and customize its appearance settings.
        Dim tb As New TextBrick()
        tb.Text = "Custom footer"
        tb.Font = New DXFont(tb.Font, DXFontStyle.Bold)
        tb.HorzAlignment = HorzAlignment.Near
        tb.Padding = New PaddingInfo(5, 0, 0, 0)
        tb.BackColor = Color.LightGray
        tb.ForeColor = Color.Black
        ' Get the client page width.
        Dim textBrickHeight As Single = e.BrickGraphics.MeasureString(tb.Text, tb.Font).Height + 4
        ' Calculate a rectangle for the brick and draw the brick.
        Dim textBrickRect As New RectangleF(0, e.Y, e.BrickGraphics.ClientPageSize.Width, textBrickHeight)
        e.BrickGraphics.DrawBrick(tb, textBrickRect)
        ' Adjust the current Y position to print the following row below the brick.
        e.Y += CInt(Fix(textBrickHeight))
    End If
    ' Add a page break.
    If (e.RowHandle + 1) Mod 10 = 0 Then
        e.PS.InsertPageBreak(e.Y)
    End If
End Sub

Private Sub SimpleButton1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SimpleButton1.Click
    GridControl1.ShowPrintPreview()
End Sub

See Also

AfterPrintRow

Export Overview

Printing Overview

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace