Articles in this section
Category / Section

How to configure the printing to fit the grid in a single page?

2 mins read

 

 

In order to print the entire grid to fit in a single page, we can override the OnPrintPage (System.Drawing.Printing.PrintPageEventArgs ev) method. This can make any number of rows and columns of the grid to be displayed in a single page.

The reason for overriding the OnPrintPage method is to fit the grid boundaries to the default page as a Bitmap image, so that it could be printed in single page.

C#

protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev)
{
   ev.HasMorePages = false;
 
   //draw a fullsize bitmap of the grid...
   int gridHeight = _grid.Model.RowHeights.GetTotal(0, _grid.Model.RowCount);
   int gridWidth = _grid.Model.ColWidths.GetTotal(0, _grid.Model.ColCount);
   Bitmap gridBM = new Bitmap(gridWidth, gridHeight);
   Graphics g = Graphics.FromImage(gridBM);
   this._grid.GridBounds = new Rectangle(0, 0, gridWidth, gridHeight);
   this._grid.DrawGrid(g);
   this._grid.ResetGridBounds();
   g.Dispose();
   Rectangle destRect = ev.MarginBounds;
 
   //use the printer graphics
   g = ev.Graphics;
 
   //draw grid bitmap into the rect on the print page
   System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point;
   RectangleF srcRect = gridBM.GetBounds(ref gu);
 
   //adjust destRect to make sizing proportional
   // you can comment this out this block if you don't want proportional fit
   float srcRatio = srcRect.Width / srcRect.Height;
   float tarRatio = (float)destRect.Width / destRect.Height;
   if (tarRatio < srcRatio)
   {
      destRect.Height = (int)(destRect.Width / srcRatio);
   }
   else
   {
      destRect.Width = (int)(destRect.Height * srcRatio);
   }
   //////////////endof proportional fit//////////////////////////
 
   g.DrawImage(gridBM, destRect, srcRect, gu);
}

 

VB

Protected Overrides Sub OnPrintPage(ByVal ev As
System.Drawing.Printing.PrintPageEventArgs)
   ev.HasMorePages = False
 
   'draw a fullsize bitmap of the grid...
   Dim gridHeight As Integer = _grid.Model.RowHeights.GetTotal(0, _grid.Model.RowCount)
   Dim gridWidth As Integer = _grid.Model.ColWidths.GetTotal(0, _grid.Model.ColCount)
   Dim gridBM As New Bitmap(gridWidth, gridHeight)
   Dim g As Graphics = Graphics.FromImage(gridBM)
   Me._grid.GridBounds = New Rectangle(0, 0, gridWidth, gridHeight)
   Me._grid.DrawGrid(g)
   Me._grid.ResetGridBounds()
   g.Dispose()
   Dim destRect As Rectangle = ev.MarginBounds
 
   'use the printer graphics
   g = ev.Graphics
 
   'draw grid bitmap into the rect on the print page
   Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point
   Dim srcRect As RectangleF = gridBM.GetBounds(gu)
 
   'adjust destRect to make sizing proportional
   ' you can comment this out this block if you don't want proportional fit
   Dim srcRatio As Single = srcRect.Width \ srcRect.Height
   Dim tarRatio As Single = CSng(destRect.Width) / destRect.Height
   If tarRatio < srcRatio Then
   destRect.Height = CInt(Fix(destRect.Width / srcRatio))
   Else
   destRect.Width = CInt(Fix(destRect.Height * srcRatio))
   End If
   '////////////endof proportional fit//////////////////////////
 
   g.DrawImage(gridBM, destRect, srcRect, gu)
End Sub
 

 

 

Showing print option for GridControl

 

 

 

 

 

Showing print preview page for GridControl

 

Sample Link:

PrintingFitToPage_C#

PrintingFitToPage_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