This sample illustrates how to print a grid on a single page. It derives a GridPrintDocument class to handle the printing of the entire grid on a single page. It does this by drawing the full-size grid to a large bitmap, and then drawing this bitmap, scaled to fit the output page.
The following code overrides OnPrintPage in the inherited GridPrintDocument class.
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev) { ev.HasMorePages = false; 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); Rectangle saveGridRect = this._grid.GridBounds; this._grid.GridBounds = new Rectangle(0, 0, gridWidth, gridHeight); this._grid.DrawGrid(g); this._grid.GridBounds = saveGridRect; g.Dispose(); Rectangle destRect = ev.MarginBounds; g = ev.Graphics; System.Drawing.GraphicsUnit gu = System.Drawing.GraphicsUnit.Point; RectangleF srcRect = gridBM.GetBounds(ref gu); g.DrawImage(gridBM, destRect, srcRect, gu); }
Features
To print only a single page, set ev.HasMorePages to false.
The bitmap height and width is set to the total height and width of the grid, inorder to draw a full-size bitmap of the grid.
The image is drawn to an instance of the Graphics class by using the DrawImage function.