using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
class DataGridViewPrinter
{
private DataGridView TheDataGridView; // The DataGridView Control which will be printed
private PrintDocument ThePrintDocument; // The PrintDocument to be used for printing
private bool IsCenterOnPage; // Determine if the report will be printed in the Top-Center of the page
private bool IsWithTitle; // Determine if the page contain title text
private string TheTitleText; // The title text to be printed in each page (if IsWithTitle is set to true)
private Font TheTitleFont; // The font to be used with the title text (if IsWithTitle is set to true)
private Color TheTitleColor; // The color to be used with the title text (if IsWithTitle is set to true)
private bool IsWithPaging; // Determine if paging is used
static int CurrentRow; // A static parameter that keep track on which Row (in the DataGridView control) that should be printed
static int PageNumber;
private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;
private float CurrentY; // A parameter that keep track on the y coordinate of the page, so the next object to be printed will start from this y coordinate
private float RowHeaderHeight;
private List<float> RowsHeight;
private List<float> ColumnsWidth;
private float TheDataGridViewWidth;
// Maintain a generic list to hold start/stop points for the column printing
// This will be used for wrapping in situations where the DataGridView will not fit on a single page
private List<int[]> mColumnPoints;
private List<float> mColumnPointsWidth;
private int mColumnPoint;
// The class constructor
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont, Color aTitleColor, bool WithPaging)
{
TheDataGridView = aDataGridView;
ThePrintDocument = aPrintDocument;
IsCenterOnPage = CenterOnPage;
IsWithTitle = WithTitle;
TheTitleText = aTitleText;
TheTitleFont = aTitleFont;
TheTitleColor = aTitleColor;
IsWithPaging = WithPaging;
PageNumber = 0;
RowsHeight = new List<float>();
ColumnsWidth = new List<float>();
mColumnPoints = new List<int[]>();
mColumnPointsWidth = new List<float>();
// Claculating the PageWidth and the PageHeight
if (!ThePrintDocument.DefaultPageSettings.Landscape)
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
else
{
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
}
// Claculating the page margins
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
// First, the current row to be printed is the first row in the DataGridView control
CurrentRow = 0;
}
// The function that calculate the height of each row (including the header row), the width of each column (according to the longest text in all its cells including the header cell), and the whole DataGridView width
private void Calculate(Graphics g)
{
if (PageNumber == 0) // Just calculate once
{
SizeF tmpSize = new SizeF();
Font tmpFont;
float tmpWidth;
TheDataGridViewWidth = 0;
for (int i = 0; i < TheDataGridView