日期:2014-05-18 浏览次数:21205 次
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Text;
namespace DGVPrinter
{
class PrintDGV
{
private static StringFormat StrFormat; // Holds content of a TextBox Cell to write by DrawString
private static StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage
private static Button CellButton; // Holds the Contents of Button Cell
private static CheckBox CellCheckBox; // Holds the Contents of CheckBox Cell
private static ComboBox CellComboBox; // Holds the Contents of ComboBox Cell
private static int TotalWidth; // Summation of Columns widths
private static int RowPos; // Position of currently printing row
private static bool NewPage; // Indicates if a new page reached
private static int PageNo; // Number of pages to print
private static ArrayList ColumnLefts = new ArrayList(); // Left Coordinate of Columns
private static ArrayList ColumnWidths = new ArrayList(); // Width of Columns
private static ArrayList ColumnTypes = new ArrayList(); // DataType of Columns
private static int CellHeight; // Height of DataGrid Cell
private static int RowsPerPage; // Number of Rows per Page
private static System.Drawing.Printing.PrintDocument printDoc =
new System.Drawing.Printing.PrintDocument(); // PrintDocumnet Object used for printing
private static string PrintTitle = ""; // Header of pages
private static DataGridView dgv; // Holds DataGridView Object to print its contents
private static List<string> SelectedColumns = new List<string>(); // The Columns Selected by user to print.
private static List<string> AvailableColumns = new List<string>(); // All Columns avaiable in DataGrid
private static bool PrintAllRows = true; // True = print all rows, False = print selected rows
private static bool FitToPageWidth = true; // True = Fits selected columns to page width , False = Print columns as showed
private static int HeaderHeight = 0;
public void Print_DataGridView(DataGridView dgv1)
{
PrintPreviewDialog ppvw;
try
{
// Getting DataGridView object to print
dgv = dgv1;
// Getting all Coulmns Names in the DataGridView
AvailableColumns.Clear();
foreach (DataGridViewColumn c in dgv.Columns)
{
if (!c.Visible) continue;
AvailableColumns.Add(c.HeaderText);
}
// Showing the PrintOption Form
PrintOptions dlg = new PrintOptions(AvailableColumns);
if (dlg.ShowDialog() != DialogResult.OK) return;
PrintTitle = dlg.PrintTitle;
PrintAllRows = dlg.PrintAllRows;
FitToPageWidth = dlg.FitToPageWidth;
SelectedColumns = dlg.GetSelectedColumns();
RowsPerPage = 0;
ppvw = new PrintPreviewDialog();
ppvw.Document = printDoc;
// Showing the Print Preview Page
printDoc.BeginPrint +=new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
printDoc.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
if (ppvw.ShowDialog() != DialogResult.OK)
{
printDoc.BeginPrint -= new System.Drawing.Printing.PrintEventHandler(PrintDoc_Beg