Home Forums WinForms controls Xceed Grid for WinForms How to print page numbers

Viewing 1 post (of 1 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17819 |

    By default, the grid is not printed with page numbers. In order to include page numbers when printing, you will need to create a class that derives from the GridPrintDocument class and override the OnPrintPage method (to be able to custom print the header or footer in this case, or other items if desired).

    The following example demonstrates the minimum implementation required in order to print page numbers when printing the grid.

    VB.NET

    Imports Xceed.Grid

    Public Class CustomGridPrintDocument
      Inherits GridPrintDocument

    Private m_grid As GridControl

    Public Sub New(ByVal grid As GridControl)
      MyBase.New(grid)
      m_grid = grid
    End Sub

    Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
          Dim gc As GraphicsContainer = e.Graphics.BeginContainer() 

          Try
             MyBase.OnPrintPage( e )
          Finally
             e.Graphics.EndContainer(gc)
          End Try 

          ‘ Print added text boxes (header and footers)
          If Not e.Cancel And Me.IsCurrentPrintingPageSelected Then
            Dim brush As New System.Drawing.SolidBrush(Color.Black)

            e.Graphics.DrawString(“Page number ” +  Me.CurrentPageNumber.ToString(), m_grid.Font, brush, New RectangleF(e.MarginBounds.X, e.MarginBounds.Y – 20, e.MarginBounds.Width, e.MarginBounds.Height))
            brush.Dispose()
          End If
    End Sub
    End Class

    C#

    using Xceed.Grid;

    public class CustomGridPrintDocument : GridPrintDocument
    {
      private GridControl m_grid;

      public CustomGridPrintDocument( GridControl grid )
        :base( grid )
      {
        m_grid = grid;
      }

      protected override void OnPrintPage( System.Drawing.Printing.PrintPageEventArgs e )
      {
        GraphicsContainer gc = e.Graphics.BeginContainer();     

        try
        {
          base.OnPrintPage( e );
        }
        finally     
        {        
          e.Graphics.EndContainer( gc );     
        }      

        // Print page number     
        if( !e.Cancel )
        {
          using( System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( Color.Black ) )
          {
            e.Graphics.DrawString(“Page number ” + this.CurrentPageNumber.ToString(), m_grid.Font, brush, new RectangleF( e.MarginBounds.X, e.MarginBounds.Y – 20, e.MarginBounds.Width, e.MarginBounds.Height ) );
          }
        }
      }
    }

    To use the custom GridPrintDocument, the following code must be used:

    VB.NET

    Dim printDocument As New CustomGridPrintDocument(GridControl1)
    printDocument.Print()

    C#

    CustomGridPrintDocument printDocument = new CustomGridPrintDocument( gridControl1 ); 
    printDocument.Print();

    Keep in mind that if you call the grid’s Print or PrintPreview methods, the grid will always be printed with the default print document.

    Imported from legacy forums. Posted by Xceed admin (had 1106 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.