Articles in this section
Category / Section

How to print two or more grids on the same page in WinForms GridControl?

2 mins read

Printing

You can print two or more grids by customizing the page layout, deriving the GridPrintDocument class and overriding its OnPrintPage method. In the override, you can draw the grids anywhere on the layout.

In the following code example, two grids are drawn on a same page by defining the particular height and weight for each grid.

 

C#

public class MyGridPrintDocument : GridPrintDocument
{
 private GridControlBase grid1;
 private GridControlBase grid2;
 public MyGridPrintDocument(GridControlBase grid1, GridControlBase grid2, bool printPreview) : base(grid1, printPreview)
  {
  this.grid1 = grid1;
  this.grid2 = grid2;
  }
 protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev)
  {
  //Do not call baseclass...
  //Set where you want the grid displayed...
  int width = grid1.Model.ColWidths.GetTotal(0, grid1.Model.ColCount);
  int height = grid1.Model.RowHeights.GetTotal(0, grid1.Model.RowCount);
  Rectangle topGridRect = new Rectangle(ev.MarginBounds.Left, ev.MarginBounds.Top, width, height);
  grid1.DrawGrid(ev.Graphics, topGridRect, false);
  //topGridRect.Inflate(1,1);
  ev.Graphics.DrawRectangle(Pens.Black, topGridRect);
  //Set where you want the second grid displayed...
  width = grid2.Model.ColWidths.GetTotal(0, grid2.Model.ColCount);
  height = grid2.Model.RowHeights.GetTotal(0, grid2.Model.RowCount);
  Rectangle botGridRect = new Rectangle(ev.MarginBounds.Left, ev.PageBounds.Height/2, width, height);
  grid2.DrawGrid(ev.Graphics, botGridRect, false);
  //botGridRect.Inflate(1,1);
  ev.Graphics.DrawRectangle(Pens.Black, botGridRect);
  }
}

 

VB

Public Class MyGridPrintDocument
        Inherits GridPrintDocument
        Private grid1 As GridControlBase
        Private grid2 As GridControlBase
        Public Sub New(ByVal grid1 As GridControlBase, ByVal grid2 As GridControlBase, ByVal printPreview As Boolean)
            MyBase.New(grid1, printPreview)
            Me.grid1 = grid1
            Me.grid2 = grid2
        End Sub
        Protected Overrides Sub OnPrintPage(ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
            'Do not call baseclass...
            'Set where you want the grid displayed...
            Dim width As Integer = grid1.Model.ColWidths.GetTotal(0, grid1.Model.ColCount)
            Dim height As Integer = grid1.Model.RowHeights.GetTotal(0, grid1.Model.RowCount)
            Dim topGridRect As New Rectangle(ev.MarginBounds.Left, ev.MarginBounds.Top, width, height)
            grid1.DrawGrid(ev.Graphics, topGridRect, False)
            'topGridRect.Inflate(1,1);
            ev.Graphics.DrawRectangle(Pens.Black, topGridRect)
            'Set where you want the second grid displayed...
            width = grid2.Model.ColWidths.GetTotal(0, grid2.Model.ColCount)
            height = grid2.Model.RowHeights.GetTotal(0, grid2.Model.RowCount)
            Dim botGridRect As New Rectangle(ev.MarginBounds.Left, ev.PageBounds.Height \ 2, width, height)
            grid2.DrawGrid(ev.Graphics, botGridRect, False)
            'botGridRect.Inflate(1,1);
            ev.Graphics.DrawRectangle(Pens.Black, botGridRect)
        End Sub
    End Class

 

Samples:

 

C#: Print Multiple Grid CS

VB: Print Multiple Grid VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied