推荐一款DataGridView的打印解决方案

摘要:
今天,一位朋友问如何在CS的WinForm中打印DataGridView中的内容。

今天有朋友请教在CS的WinForm中如何打印DataGridView中的内容。
网上搜索一番之后,还是在藏宝库CodeProject中找到一篇好文章《DataGridView Printing by Selecting Columns and Rows》(http://www.codeproject.com/KB/grid/PrintDataGrid_CS.aspx

效果图
【打印设置画面】

推荐一款DataGridView的打印解决方案第1张

【打印预览画面】
推荐一款DataGridView的打印解决方案第2张

解决方案构成
这个打印解决方案由一个打印设置的窗体,及一个打印类组成。
可用于以下场景:
1、显示的数据量较大,但又没有必要打印全部数据的时候
2、希望打印出的列宽能自动适应页面宽度

打印类主要方法
Print_DataGridView(共有): 被外部类调用的主方法.
PrintDoc_BeginPrint(私有): 初始化一些打印变量
PrintDoc_PrintPage(私有): 执行打印工作
DrawFooter(私有): 页脚的处理部分

打印类代码

推荐一款DataGridView的打印解决方案第3张推荐一款DataGridView的打印解决方案第4张/**//* ***************************************************
推荐一款DataGridView的打印解决方案第5张 * DataGridView打印类
推荐一款DataGridView的打印解决方案第5张 * 原作者:Afrasiab Cheraghi. 
推荐一款DataGridView的打印解决方案第5张 * 修改者:何奎
推荐一款DataGridView的打印解决方案第5张 * 
推荐一款DataGridView的打印解决方案第9张 * *************************************************
*/

推荐一款DataGridView的打印解决方案第10张
推荐一款DataGridView的打印解决方案第10张
using System;
推荐一款DataGridView的打印解决方案第10张
using System.Collections.Generic;
推荐一款DataGridView的打印解决方案第10张
using System.Windows.Forms;
推荐一款DataGridView的打印解决方案第10张
using System.Drawing;
推荐一款DataGridView的打印解决方案第10张
using System.Collections;
推荐一款DataGridView的打印解决方案第10张
using System.Data;
推荐一款DataGridView的打印解决方案第10张
using System.Text;
推荐一款DataGridView的打印解决方案第10张
推荐一款DataGridView的打印解决方案第10张
namespace testPrint
推荐一款DataGridView的打印解决方案第3张推荐一款DataGridView的打印解决方案第4张
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张    
class PrintDGV
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张    
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张        
private static StringFormat StrFormat;  // Holds content of a TextBox Cell to write by DrawString
推荐一款DataGridView的打印解决方案第5张
        private static StringFormat StrFormatComboBox; // Holds content of a Boolean Cell to write by DrawImage
推荐一款DataGridView的打印解决方案第5张
        private static Button CellButton;       // Holds the Contents of Button Cell
推荐一款DataGridView的打印解决方案第5张
        private static CheckBox CellCheckBox;   // Holds the Contents of CheckBox Cell 
推荐一款DataGridView的打印解决方案第5张
        private static ComboBox CellComboBox;   // Holds the Contents of ComboBox Cell
推荐一款DataGridView的打印解决方案第5张

推荐一款DataGridView的打印解决方案第5张        
private static int TotalWidth;          // Summation of Columns widths
推荐一款DataGridView的打印解决方案第5张
        private static int RowPos;              // Position of currently printing row 
推荐一款DataGridView的打印解决方案第5张
        private static bool NewPage;            // Indicates if a new page reached
推荐一款DataGridView的打印解决方案第5张
        private static int PageNo;              // Number of pages to print
推荐一款DataGridView的打印解决方案第5张
        private static ArrayList ColumnLefts = new ArrayList();  // Left Coordinate of Columns
推荐一款DataGridView的打印解决方案第5张
        private static ArrayList ColumnWidths = new ArrayList(); // Width of Columns
推荐一款DataGridView的打印解决方案第5张
        private static ArrayList ColumnTypes = new ArrayList();  // DataType of Columns
推荐一款DataGridView的打印解决方案第5张
        private static int CellHeight;          // Height of DataGrid Cell
推荐一款DataGridView的打印解决方案第5张
        private static int RowsPerPage;         // Number of Rows per Page
推荐一款DataGridView的打印解决方案第5张
        private static System.Drawing.Printing.PrintDocument printDoc =
推荐一款DataGridView的打印解决方案第5张                       
new System.Drawing.Printing.PrintDocument();  // PrintDocumnet Object used for printing
推荐一款DataGridView的打印解决方案第5张

推荐一款DataGridView的打印解决方案第5张        
private static string PrintTitle = "";  // Header of pages
推荐一款DataGridView的打印解决方案第5张
        private static DataGridView dgv;        // Holds DataGridView Object to print its contents
推荐一款DataGridView的打印解决方案第5张
        private static List<string> SelectedColumns = new List<string>();   // The Columns Selected by user to print.
推荐一款DataGridView的打印解决方案第5张
        private static List<string> AvailableColumns = new List<string>();  // All Columns avaiable in DataGrid 
推荐一款DataGridView的打印解决方案第5张
        private static bool PrintAllRows = true;   // True = print all rows,  False = print selected rows    
推荐一款DataGridView的打印解决方案第5张
        private static bool FitToPageWidth = true// True = Fits selected columns to page width ,  False = Print columns as showed    
推荐一款DataGridView的打印解决方案第5张
        private static int HeaderHeight = 0;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张        
public static void Print_DataGridView(DataGridView dgv1)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张            PrintPreviewDialog ppvw;
推荐一款DataGridView的打印解决方案第5张            
try 
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{    
推荐一款DataGridView的打印解决方案第5张                
// Getting DataGridView object to print
推荐一款DataGridView的打印解决方案第5张
                dgv = dgv1;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Getting all Coulmns Names in the DataGridView
推荐一款DataGridView的打印解决方案第5张
                AvailableColumns.Clear();
推荐一款DataGridView的打印解决方案第5张                
foreach (DataGridViewColumn c in dgv.Columns)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                    
if (!c.Visible) continue;
推荐一款DataGridView的打印解决方案第5张                    AvailableColumns.Add(c.HeaderText);
推荐一款DataGridView的打印解决方案第73张                }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Showing the PrintOption Form
推荐一款DataGridView的打印解决方案第5张
                PrintOptions dlg = new PrintOptions(AvailableColumns);
推荐一款DataGridView的打印解决方案第5张                
if (dlg.ShowDialog() != DialogResult.OK) return;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                PrintTitle 
= dlg.PrintTitle;
推荐一款DataGridView的打印解决方案第5张                PrintAllRows 
= dlg.PrintAllRows;
推荐一款DataGridView的打印解决方案第5张                FitToPageWidth 
= dlg.FitToPageWidth;
推荐一款DataGridView的打印解决方案第5张                SelectedColumns 
= dlg.GetSelectedColumns();
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                RowsPerPage 
= 0;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                ppvw 
= new PrintPreviewDialog();
推荐一款DataGridView的打印解决方案第5张                ppvw.Document 
= printDoc;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Showing the Print Preview Page
推荐一款DataGridView的打印解决方案第5张
                printDoc.BeginPrint +=new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
推荐一款DataGridView的打印解决方案第5张                printDoc.PrintPage 
+=new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
推荐一款DataGridView的打印解决方案第5张                
if (ppvw.ShowDialog() != DialogResult.OK)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                    printDoc.BeginPrint 
-= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
推荐一款DataGridView的打印解决方案第5张                    printDoc.PrintPage 
-= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
推荐一款DataGridView的打印解决方案第5张                    
return;
推荐一款DataGridView的打印解决方案第73张                }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Printing the Documnet
推荐一款DataGridView的打印解决方案第5张
                printDoc.Print();
推荐一款DataGridView的打印解决方案第5张                printDoc.BeginPrint 
-= new System.Drawing.Printing.PrintEventHandler(PrintDoc_BeginPrint);
推荐一款DataGridView的打印解决方案第5张                printDoc.PrintPage 
-= new System.Drawing.Printing.PrintPageEventHandler(PrintDoc_PrintPage);
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第5张            
catch (Exception ex)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第5张            
finally
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第73张        }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张        
private static void PrintDoc_BeginPrint(object sender, 
推荐一款DataGridView的打印解决方案第5张                    System.Drawing.Printing.PrintEventArgs e) 
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张            
try
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                
// Formatting the Content of Text Cell to print
推荐一款DataGridView的打印解决方案第5张
                StrFormat = new StringFormat();
推荐一款DataGridView的打印解决方案第5张                StrFormat.Alignment 
= StringAlignment.Near;
推荐一款DataGridView的打印解决方案第5张                StrFormat.LineAlignment 
= StringAlignment.Center;
推荐一款DataGridView的打印解决方案第5张                StrFormat.Trimming 
= StringTrimming.EllipsisCharacter;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Formatting the Content of Combo Cells to print
推荐一款DataGridView的打印解决方案第5张
                StrFormatComboBox = new StringFormat();
推荐一款DataGridView的打印解决方案第5张                StrFormatComboBox.LineAlignment 
= StringAlignment.Center;
推荐一款DataGridView的打印解决方案第5张                StrFormatComboBox.FormatFlags 
= StringFormatFlags.NoWrap;
推荐一款DataGridView的打印解决方案第5张                StrFormatComboBox.Trimming 
= StringTrimming.EllipsisCharacter;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                ColumnLefts.Clear();
推荐一款DataGridView的打印解决方案第5张                ColumnWidths.Clear();
推荐一款DataGridView的打印解决方案第5张                ColumnTypes.Clear();
推荐一款DataGridView的打印解决方案第5张                CellHeight 
= 0;
推荐一款DataGridView的打印解决方案第5张                RowsPerPage 
= 0;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// For various column types
推荐一款DataGridView的打印解决方案第5张
                CellButton = new Button();
推荐一款DataGridView的打印解决方案第5张                CellCheckBox 
= new CheckBox();
推荐一款DataGridView的打印解决方案第5张                CellComboBox 
= new ComboBox();
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Calculating Total Widths
推荐一款DataGridView的打印解决方案第5张
                TotalWidth = 0;
推荐一款DataGridView的打印解决方案第5张                
foreach (DataGridViewColumn GridCol in dgv.Columns)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                    
if (!GridCol.Visible) continue;
推荐一款DataGridView的打印解决方案第5张                    
if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;
推荐一款DataGridView的打印解决方案第5张                    TotalWidth 
+= GridCol.Width;
推荐一款DataGridView的打印解决方案第73张                }

推荐一款DataGridView的打印解决方案第5张                PageNo 
= 1;
推荐一款DataGridView的打印解决方案第5张                NewPage 
= true;
推荐一款DataGridView的打印解决方案第5张                RowPos 
= 0;                
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第5张            
catch (Exception ex)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第73张        }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张        
private static void PrintDoc_PrintPage(object sender, 
推荐一款DataGridView的打印解决方案第5张                    System.Drawing.Printing.PrintPageEventArgs e) 
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张            
int tmpWidth, i;
推荐一款DataGridView的打印解决方案第5张            
int tmpTop = e.MarginBounds.Top;
推荐一款DataGridView的打印解决方案第5张            
int tmpLeft = e.MarginBounds.Left;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张            
try 
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{            
推荐一款DataGridView的打印解决方案第5张                
// Before starting first page, it saves Width & Height of Headers and CoulmnType
推荐一款DataGridView的打印解决方案第5张
                if (PageNo == 1
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                    
foreach (DataGridViewColumn GridCol in dgv.Columns)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                    
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                        
if (!GridCol.Visible) continue;
推荐一款DataGridView的打印解决方案第5张                        
// Skip if the current column not selected
推荐一款DataGridView的打印解决方案第5张
                        if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) continue;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                        
// Detemining whether the columns are fitted to page or not.
推荐一款DataGridView的打印解决方案第5张
                        if (FitToPageWidth) 
推荐一款DataGridView的打印解决方案第5张                            tmpWidth 
= (int)(Math.Floor((double)((double)GridCol.Width / 
推荐一款DataGridView的打印解决方案第5张                                       (
double)TotalWidth * (double)TotalWidth * 
推荐一款DataGridView的打印解决方案第5张                                       ((
double)e.MarginBounds.Width / (double)TotalWidth))));
推荐一款DataGridView的打印解决方案第5张                        
else
推荐一款DataGridView的打印解决方案第5张                            tmpWidth 
= GridCol.Width;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                        HeaderHeight 
= (int)(e.Graphics.MeasureString(GridCol.HeaderText,
推荐一款DataGridView的打印解决方案第5张                                    GridCol.InheritedStyle.Font, tmpWidth).Height) 
+ 11;
推荐一款DataGridView的打印解决方案第5张                        
推荐一款DataGridView的打印解决方案第5张                        
// Save width & height of headres and ColumnType
推荐一款DataGridView的打印解决方案第5张
                        ColumnLefts.Add(tmpLeft);
推荐一款DataGridView的打印解决方案第5张                        ColumnWidths.Add(tmpWidth);
推荐一款DataGridView的打印解决方案第5张                        ColumnTypes.Add(GridCol.GetType());
推荐一款DataGridView的打印解决方案第5张                        tmpLeft 
+= tmpWidth;
推荐一款DataGridView的打印解决方案第73张                    }

推荐一款DataGridView的打印解决方案第73张                }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Printing Current Page, Row by Row
推荐一款DataGridView的打印解决方案第5张
                while (RowPos <= dgv.Rows.Count - 1)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                    DataGridViewRow GridRow 
= dgv.Rows[RowPos];
推荐一款DataGridView的打印解决方案第5张                    
if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                    
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                        RowPos
++;
推荐一款DataGridView的打印解决方案第5张                        
continue;
推荐一款DataGridView的打印解决方案第73张                    }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                    CellHeight 
= GridRow.Height;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                    
if (tmpTop + CellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                    
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                        DrawFooter(e, RowsPerPage);
推荐一款DataGridView的打印解决方案第5张                        NewPage 
= true;
推荐一款DataGridView的打印解决方案第5张                        PageNo
++;
推荐一款DataGridView的打印解决方案第5张                        e.HasMorePages 
= true;
推荐一款DataGridView的打印解决方案第5张                        
return;
推荐一款DataGridView的打印解决方案第73张                    }

推荐一款DataGridView的打印解决方案第5张                    
else
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                    
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                        
if (NewPage)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                            
// Draw Header
推荐一款DataGridView的打印解决方案第5张
                            e.Graphics.DrawString(PrintTitle, new Font(dgv.Font, FontStyle.Bold), 
推荐一款DataGridView的打印解决方案第5张                                    Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top 
-
推荐一款DataGridView的打印解决方案第5张                            e.Graphics.MeasureString(PrintTitle, 
new Font(dgv.Font, 
推荐一款DataGridView的打印解决方案第5张                                    FontStyle.Bold), e.MarginBounds.Width).Height 
- 13);
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            String s 
= DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            e.Graphics.DrawString(s, 
new Font(dgv.Font, FontStyle.Bold), 
推荐一款DataGridView的打印解决方案第5张                                    Brushes.Black, e.MarginBounds.Left 
+ (e.MarginBounds.Width - 
推荐一款DataGridView的打印解决方案第5张                                    e.Graphics.MeasureString(s, 
new Font(dgv.Font, 
推荐一款DataGridView的打印解决方案第5张                                    FontStyle.Bold), e.MarginBounds.Width).Width), e.MarginBounds.Top 
- 
推荐一款DataGridView的打印解决方案第5张                                    e.Graphics.MeasureString(PrintTitle, 
new Font(new Font(dgv.Font, 
推荐一款DataGridView的打印解决方案第5张                                    FontStyle.Bold), FontStyle.Bold), e.MarginBounds.Width).Height 
- 13);
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            
// Draw Columns
推荐一款DataGridView的打印解决方案第5张
                            tmpTop = e.MarginBounds.Top;
推荐一款DataGridView的打印解决方案第5张                            i 
= 0;
推荐一款DataGridView的打印解决方案第5张                            
foreach (DataGridViewColumn GridCol in dgv.Columns)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                
if (!GridCol.Visible) continue;
推荐一款DataGridView的打印解决方案第5张                                
if (!PrintDGV.SelectedColumns.Contains(GridCol.HeaderText)) 
推荐一款DataGridView的打印解决方案第5张                                    
continue;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.FillRectangle(
new SolidBrush(Color.LightGray), 
推荐一款DataGridView的打印解决方案第5张                                    
new Rectangle((int) ColumnLefts[i], tmpTop,
推荐一款DataGridView的打印解决方案第5张                                    (
int)ColumnWidths[i], HeaderHeight));
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawRectangle(Pens.Black, 
推荐一款DataGridView的打印解决方案第5张                                    
new Rectangle((int) ColumnLefts[i], tmpTop,
推荐一款DataGridView的打印解决方案第5张                                    (
int)ColumnWidths[i], HeaderHeight));
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, 
推荐一款DataGridView的打印解决方案第5张                                    
new SolidBrush(GridCol.InheritedStyle.ForeColor),
推荐一款DataGridView的打印解决方案第5张                                    
new RectangleF((int)ColumnLefts[i], tmpTop, 
推荐一款DataGridView的打印解决方案第5张                                    (
int)ColumnWidths[i], HeaderHeight), StrFormat);
推荐一款DataGridView的打印解决方案第5张                                i
++;
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张                            NewPage 
= false;
推荐一款DataGridView的打印解决方案第5张                            tmpTop 
+= HeaderHeight;
推荐一款DataGridView的打印解决方案第73张                        }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                        
// Draw Columns Contents
推荐一款DataGridView的打印解决方案第5张
                        i = 0;
推荐一款DataGridView的打印解决方案第5张                        
foreach (DataGridViewCell Cel in GridRow.Cells)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                            
if (!Cel.OwningColumn.Visible) continue;
推荐一款DataGridView的打印解决方案第5张                            
if (!SelectedColumns.Contains(Cel.OwningColumn.HeaderText))
推荐一款DataGridView的打印解决方案第5张                                
continue;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            
// For the TextBox Column
推荐一款DataGridView的打印解决方案第5张
                            if (((Type) ColumnTypes[i]).Name == "DataGridViewTextBoxColumn" || 
推荐一款DataGridView的打印解决方案第5张                                ((Type) ColumnTypes[i]).Name 
== "DataGridViewLinkColumn")
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, 
推荐一款DataGridView的打印解决方案第5张                                        
new SolidBrush(Cel.InheritedStyle.ForeColor),
推荐一款DataGridView的打印解决方案第5张                                        
new RectangleF((int)ColumnLefts[i], (float)tmpTop,
推荐一款DataGridView的打印解决方案第5张                                        (
int)ColumnWidths[i], (float)CellHeight), StrFormat);
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张                            
// For the Button Column
推荐一款DataGridView的打印解决方案第5张
                            else if (((Type) ColumnTypes[i]).Name == "DataGridViewButtonColumn")
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                CellButton.Text 
= Cel.Value.ToString();
推荐一款DataGridView的打印解决方案第5张                                CellButton.Size 
= new Size((int)ColumnWidths[i], CellHeight);
推荐一款DataGridView的打印解决方案第5张                                Bitmap bmp 
=new Bitmap(CellButton.Width, CellButton.Height);
推荐一款DataGridView的打印解决方案第5张                                CellButton.DrawToBitmap(bmp, 
new Rectangle(00
推荐一款DataGridView的打印解决方案第5张                                        bmp.Width, bmp.Height));
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawImage(bmp, 
new Point((int)ColumnLefts[i], tmpTop));
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张                            
// For the CheckBox Column
推荐一款DataGridView的打印解决方案第5张
                            else if (((Type) ColumnTypes[i]).Name == "DataGridViewCheckBoxColumn")
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                CellCheckBox.Size 
= new Size(1414);
推荐一款DataGridView的打印解决方案第5张                                CellCheckBox.Checked 
= (bool)Cel.Value;
推荐一款DataGridView的打印解决方案第5张                                Bitmap bmp 
= new Bitmap((int)ColumnWidths[i], CellHeight);
推荐一款DataGridView的打印解决方案第5张                                Graphics tmpGraphics 
= Graphics.FromImage(bmp);
推荐一款DataGridView的打印解决方案第5张                                tmpGraphics.FillRectangle(Brushes.White, 
new Rectangle(00
推荐一款DataGridView的打印解决方案第5张                                        bmp.Width, bmp.Height));
推荐一款DataGridView的打印解决方案第5张                                CellCheckBox.DrawToBitmap(bmp, 
推荐一款DataGridView的打印解决方案第5张                                        
new Rectangle((int)((bmp.Width - CellCheckBox.Width) / 2), 
推荐一款DataGridView的打印解决方案第5张                                        (
int)((bmp.Height - CellCheckBox.Height) / 2), 
推荐一款DataGridView的打印解决方案第5张                                        CellCheckBox.Width, CellCheckBox.Height));
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawImage(bmp, 
new Point((int)ColumnLefts[i], tmpTop));
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张                            
// For the ComboBox Column
推荐一款DataGridView的打印解决方案第5张
                            else if (((Type) ColumnTypes[i]).Name == "DataGridViewComboBoxColumn")
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                CellComboBox.Size 
= new Size((int)ColumnWidths[i], CellHeight);
推荐一款DataGridView的打印解决方案第5张                                Bitmap bmp 
= new Bitmap(CellComboBox.Width, CellComboBox.Height);
推荐一款DataGridView的打印解决方案第5张                                CellComboBox.DrawToBitmap(bmp, 
new Rectangle(00
推荐一款DataGridView的打印解决方案第5张                                        bmp.Width, bmp.Height));
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawImage(bmp, 
new Point((int)ColumnLefts[i], tmpTop));
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawString(Cel.Value.ToString(), Cel.InheritedStyle.Font, 
推荐一款DataGridView的打印解决方案第5张                                        
new SolidBrush(Cel.InheritedStyle.ForeColor), 
推荐一款DataGridView的打印解决方案第5张                                        
new RectangleF((int)ColumnLefts[i] + 1, tmpTop, (int)ColumnWidths[i]
推荐一款DataGridView的打印解决方案第5张                                        
- 16, CellHeight), StrFormatComboBox);
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张                            
// For the Image Column
推荐一款DataGridView的打印解决方案第5张
                            else if (((Type) ColumnTypes[i]).Name == "DataGridViewImageColumn")
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张                            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                                Rectangle CelSize 
= new Rectangle((int)ColumnLefts[i], 
推荐一款DataGridView的打印解决方案第5张                                        tmpTop, (
int)ColumnWidths[i], CellHeight);
推荐一款DataGridView的打印解决方案第5张                                Size ImgSize 
= ((Image)(Cel.FormattedValue)).Size;
推荐一款DataGridView的打印解决方案第5张                                e.Graphics.DrawImage((Image)Cel.FormattedValue, 
推荐一款DataGridView的打印解决方案第5张                                        
new Rectangle((int)ColumnLefts[i] + (int)((CelSize.Width - ImgSize.Width) / 2), 
推荐一款DataGridView的打印解决方案第5张                                        tmpTop 
+ (int)((CelSize.Height - ImgSize.Height) / 2), 
推荐一款DataGridView的打印解决方案第5张                                        ((Image)(Cel.FormattedValue)).Width, ((Image)(Cel.FormattedValue)).Height));
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第73张                            }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            
// Drawing Cells Borders 
推荐一款DataGridView的打印解决方案第5张
                            e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)ColumnLefts[i], 
推荐一款DataGridView的打印解决方案第5张                                    tmpTop, (
int)ColumnWidths[i], CellHeight));
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                            i
++;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第73张                        }

推荐一款DataGridView的打印解决方案第5张                        tmpTop 
+= CellHeight;
推荐一款DataGridView的打印解决方案第73张                    }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                    RowPos
++;
推荐一款DataGridView的打印解决方案第5张                    
// For the first page it calculates Rows per Page
推荐一款DataGridView的打印解决方案第5张
                    if (PageNo == 1) RowsPerPage++;
推荐一款DataGridView的打印解决方案第73张                }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
if (RowsPerPage == 0return;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                
// Write Footer (Page Number)
推荐一款DataGridView的打印解决方案第5张
                DrawFooter(e, RowsPerPage);
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张                e.HasMorePages 
= false;
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第5张            
catch (Exception ex)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                MessageBox.Show(ex.Message, 
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
推荐一款DataGridView的打印解决方案第73张            }

推荐一款DataGridView的打印解决方案第73张        }

推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张        
private static void DrawFooter(System.Drawing.Printing.PrintPageEventArgs e, 
推荐一款DataGridView的打印解决方案第5张                    
int RowsPerPage)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张        
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张            
double cnt = 0
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张            
// Detemining rows number to print
推荐一款DataGridView的打印解决方案第5张
            if (PrintAllRows)
推荐一款DataGridView的打印解决方案第24张推荐一款DataGridView的打印解决方案第25张            
推荐一款DataGridView的打印解决方案第22张{
推荐一款DataGridView的打印解决方案第5张                
if (dgv.Rows[dgv.Rows.Count - 1].IsNewRow) 
推荐一款DataGridView的打印解决方案第5张                    cnt 
= dgv.Rows.Count - 2// When the DataGridView doesn't allow adding rows
推荐一款DataGridView的打印解决方案第5张
                else
推荐一款DataGridView的打印解决方案第5张                    cnt 
= dgv.Rows.Count - 1// When the DataGridView allows adding rows
推荐一款DataGridView的打印解决方案第73张
            }

推荐一款DataGridView的打印解决方案第5张            
else
推荐一款DataGridView的打印解决方案第5张                cnt 
= dgv.SelectedRows.Count;
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张            
// Writing the Page Number on the Bottom of Page
推荐一款DataGridView的打印解决方案第5张
            string PageNum = " 第 " + PageNo.ToString()
推荐一款DataGridView的打印解决方案第5张                           
+ " 页,共 " + Math.Ceiling((double)(cnt / RowsPerPage)).ToString()
推荐一款DataGridView的打印解决方案第5张                           
+ " 页";
推荐一款DataGridView的打印解决方案第5张
推荐一款DataGridView的打印解决方案第5张            e.Graphics.DrawString(PageNum, dgv.Font, Brushes.Black, 
推荐一款DataGridView的打印解决方案第5张                e.MarginBounds.Left 
+ (e.MarginBounds.Width - 
推荐一款DataGridView的打印解决方案第5张                e.Graphics.MeasureString(PageNum, dgv.Font, 
推荐一款DataGridView的打印解决方案第5张                e.MarginBounds.Width).Width) 
/ 2, e.MarginBounds.Top + 
推荐一款DataGridView的打印解决方案第5张                e.MarginBounds.Height 
+ 31);
推荐一款DataGridView的打印解决方案第73张        }

推荐一款DataGridView的打印解决方案第73张    }

推荐一款DataGridView的打印解决方案第9张}

免责声明:文章转载自《推荐一款DataGridView的打印解决方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇xv6 makefilewebsocket长连接压力测试踩过的坑下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

DataGridView很详细的用法

DataGridView很详细的用法  一、 DataGridView 取得或者修改当前单元格的内容: 当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回Nothing(C#是null) // 取得当前单元格内容 Cons...

C#中对Winform中的DataGridView的控制技巧。(单独控制某单元格的按钮不显示、某单元格的ReadOnly)

1:控制按钮列中的某一行不显示按钮。(使用环境:数据的移动,如把第二行的数据移动到上面去,最下面的一行为合计行,不允许移动,因此,就需要把第一行与最后一行的按钮屏蔽掉。屏蔽的方法:把按钮改成普通的单元格。具体实现代码如下:(第8列中的第一行与最后一行按钮不显示)) //定义绘画表格前的事件,在绘画前把按钮转换成普通单元格。dataGrid.RowPreP...

双行表头DatagridView的简单实现

DatagridView默认不支持多行表头的实现,一些第三方的控件,比如Spread就可以,因此要实现这个功能,只能自己想办法了。介绍两种思路:1,用重写DataGridView的Paint等方法,可以重画表头,这个实现起来较为复杂,费时费力,此处略去。2,利用两个DataGridView,一个DataGridView用来显示第一行标题,另一个Datagr...

转载:winform的DataGridView中用C#实现按钮列置灰

DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显示具有类似按钮的用户界面 (UI) 的单元格。但 DataGridViewButtonCell 不提供禁用由单元格显示的按钮外观的方式。下面的代码示例演示如何自定义 DataGridViewButtonCell 类来显示可以显示为禁用的按钮。本示例定义一个新的单...

编辑datagridview单元格

以这3种为例,最简单的是第三种,直接让单元格处于可编辑状态,当完成编辑后触发CellEndEdit事件,最后对输入的数据进行处理。 1 private DateTimePicker dtp = new DateTimePicker(); 2 private ComBox sellstyle = new ComBox ();//设置全局变量 View C...

Winform传统DataGridView和DevExpress控件的GridControl两者表头全选功能的实现

在开发一个个人项目的时候,有客户反映默认GridView多选操作不是很方便和理想,想在列表的左边增加一列可以勾选,并且最好支持列表头部全选的操作,否则数据多的时候一个个勾选要到天荒地老。 基于以上需求,找了不少例子进行比较,并对代码进行测试改进,终于完成了以上的功能了, 并且由于我本身做了多套界面的处理,因此,基于传统的DataGridView全选操作不能...