Click here to Skip to main content
15,894,546 members

Comments by Mehdi Formi (Top 1 by date)

Mehdi Formi 28-May-14 4:18am View    
public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument,
bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont,
Color aTitleColor, bool WithPaging)

// The DataGridView Control which will be printed.
DataGridView MyDataGridView;
// The PrintDocument to be used for printing.
PrintDocument MyPrintDocument;
// The class that will do the printing process.
DataGridViewPrinter MyDataGridViewPrinter;



/ The printing setup function
private bool SetupThePrinting()
{
PrintDialog MyPrintDialog = new PrintDialog();
MyPrintDialog.AllowCurrentPage = false;
MyPrintDialog.AllowPrintToFile = false;
MyPrintDialog.AllowSelection = false;
MyPrintDialog.AllowSomePages = false;
MyPrintDialog.PrintToFile = false;
MyPrintDialog.ShowHelp = false;
MyPrintDialog.ShowNetwork = false;

if (MyPrintDialog.ShowDialog() != DialogResult.OK)
return false;

MyPrintDocument.DocumentName = "Customers Report";
MyPrintDocument.PrinterSettings =
MyPrintDialog.PrinterSettings;
MyPrintDocument.DefaultPageSettings =
MyPrintDialog.PrinterSettings.DefaultPageSettings;
MyPrintDocument.DefaultPageSettings.Margins =
new Margins(40, 40, 40, 40);

if (MessageBox.Show("Do you want the report to be centered on the page",
"InvoiceManager - Center on Page", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView,
MyPrintDocument, true, true, "Customers", new Font("Tahoma", 18,
FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
else
MyDataGridViewPrinter = new DataGridViewPrinter(MyDataGridView,
MyPrintDocument, false, true, "Customers", new Font("Tahoma", 18,
FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);

return true;


// The Print Button
private void btnPrint_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
MyPrintDocument.Print();
}

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
{
// The DataGridView Control which will be printed
private DataGridView TheDataGridView;
// The PrintDocument to be used for printing
private PrintDocument ThePrintDocument;
// Determine if the report will be
// printed in the Top-Center of the page
private bool IsCenterOnPage;
// Determine if the page contain title text
private bool IsWithTitle;
// The title text to be printed
// in each page (if IsWithTitle is set to true)
private string TheTitleText;
// The font to be used with the title
// text (if IsWithTitle is set to true)
private Font TheTitleFont;
// The color to be used with the title
// text (if IsWithTitle is set to true)
private Color TheTitleColor;
// Determine if paging is used
private bool IsWithPaging;

// A static parameter that keep track
// on which Row (in the DataGridView control)
// that should be printed
static int CurrentRow;

static int PageNumber;

private int PageWidth;
private int PageHeight;
private int LeftMargin;
private int TopMargin;
private int RightMargin;
private int BottomMargin;

// 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 CurrentY;

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