65.9K
CodeProject 正在变化。 阅读更多。
Home

DataGridViewPrinter 类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.91/5 (148投票s)

2006年4月4日

3分钟阅读

viewsIcon

1067279

downloadIcon

50298

DataGridView 的打印类

Sample Image - DataGridViewPrinterApplication.jpg

引言

.NET 2.0 中的 DataGridView 控件是一个惊人的数据表示控件,包含许多我们可以从中受益的高级功能。此控件唯一不支持的功能是打印功能。我在网上搜索了这样的功能,但没有找到任何真正好的,而且我找到的大多数打印类都是用于打印 DataTable 对象或打印传统的 DataGrid 控件。因此,我决定为这个功能创建自己的代码并与他人分享。

类的特性

  • 打印样式几乎与 DataGridView 控件的样式相同
    • 标题和其他行的相同字体样式
    • 标题和其他行的相同前景色和背景色样式
    • 行的相同交替背景样式
    • 将考虑特定行的特殊字体
    • 将考虑特定行的特殊前景色和背景色样式
    • 列的相同对齐方式
  • 支持多页
  • 计算要打印的每列的宽度以适合所有单元格(包括标题单元格)
  • 可以指定页面顶部的标题
  • 可以指定标题字体和颜色
  • 标题和标题行在每个页面中重复
  • 报表可以位于页面顶部居中(考虑到页面的顶部边距),也可以与左上边距对齐
  • 打印过程忽略任何不可见的行或列(假设用户不希望它们出现)
  • 如果 DataGridView 宽度大于页面宽度,则 x 坐标大于页面宽度的列将打印到另一页。这确保了所有列都将被打印(感谢 Stephen Long
  • 支持页码
  • 打印过程使用 Graphics.MeasureString 来计算具有指定字体的特定文本的高度和宽度。这确保了打印的精确性。
  • 该类支持从右到左的字体

类构造函数

public DataGridViewPrinter(DataGridView aDataGridView, PrintDocument aPrintDocument,
    bool CenterOnPage, bool WithTitle, string aTitleText, Font aTitleFont,
    Color aTitleColor, bool WithPaging)
  • aDataGridView:要打印的 DataGridView 控件
  • aPrintDocument:用于打印的 PrintDocument
  • CenterOnPage:确定报表是否将打印在页面顶部中心
  • WithTitle:确定页面是否包含标题文本
  • aTitleText:要在每个页面中打印的标题文本(如果 WithTitle 设置为 true
  • aTitleFont:用于标题文本的字体(如果 WithTitle 设置为 true
  • aTitleColor:用于标题文本的颜色(如果 WithTitle 设置为 true
  • 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();
}
// The PrintPage action for the PrintDocument control
private void MyPrintDocument_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
{
    bool more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics);
    if (more == true)
        e.HasMorePages = true;
}
// The Print Preview Button
private void btnPrintPreview_Click(object sender, EventArgs e)
{
    if (SetupThePrinting())
    {
        PrintPreviewDialog MyPrintPreviewDialog = new PrintPreviewDialog();
        MyPrintPreviewDialog.Document = MyPrintDocument;
        MyPrintPreviewDialog.ShowDialog();
    }
}

请参阅包含 DataGridViewPrinter 类(.cs 文件)的演示项目和一个显示如何使用它的简单示例。

DataGridViewPrinter 类源代码

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 keeps 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 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>();

        // Calculating 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;
        }

        // Calculating 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 calculates
    // 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.Columns.Count; i++)
            {
                tmpFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
                if (tmpFont == null)
                // If there is no special HeaderFont style,
                // then use the default DataGridView font style
                    tmpFont = TheDataGridView.DefaultCellStyle.Font;

                tmpSize = g.MeasureString(
                          TheDataGridView.Columns[i].HeaderText, 
                          tmpFont);
                tmpWidth = tmpSize.Width;
                RowHeaderHeight = tmpSize.Height;

                for (int j = 0; j < TheDataGridView.Rows.Count; j++)
                {
                    tmpFont = TheDataGridView.Rows[j].DefaultCellStyle.Font;
                    if (tmpFont == null)
                    // If the there is no special font style of the
                    // CurrentRow, then use the default one associated
                    // with the DataGridView control
                        tmpFont = TheDataGridView.DefaultCellStyle.Font;

                    tmpSize = g.MeasureString("Anything", tmpFont);
                    RowsHeight.Add(tmpSize.Height);

                    tmpSize =
                        g.MeasureString(
                        TheDataGridView.Rows[j].Cells[i].
                                 EditedFormattedValue.ToString(),
                        tmpFont);
                    if (tmpSize.Width > tmpWidth)
                        tmpWidth = tmpSize.Width;
                }
                if (TheDataGridView.Columns[i].Visible)
                    TheDataGridViewWidth += tmpWidth;
                ColumnsWidth.Add(tmpWidth);
            }

            // Define the start/stop column points
            // based on the page width and
            // the DataGridView Width
            // We will use this to determine
            // the columns which are drawn on each page
            // and how wrapping will be handled
            // By default, the wrapping will occurr
            // such that the maximum number of
            // columns for a page will be determined
            int k;

            int mStartPoint = 0;
            for (k = 0; k < TheDataGridView.Columns.Count; k++)
                if (TheDataGridView.Columns[k].Visible)
                {
                    mStartPoint = k;
                    break;
                }

            int mEndPoint = TheDataGridView.Columns.Count;
            for (k = TheDataGridView.Columns.Count - 1; k >= 0; k--)
                if (TheDataGridView.Columns[k].Visible)
                {
                    mEndPoint = k + 1;
                    break;
                }

            float mTempWidth = TheDataGridViewWidth;
            float mTempPrintArea = (float)PageWidth - (float)LeftMargin -
                (float)RightMargin;
            
            // We only care about handling
            // where the total datagridview width is bigger
            // then the print area
            if (TheDataGridViewWidth > mTempPrintArea)
            {
                mTempWidth = 0.0F;
                for (k = 0; k < TheDataGridView.Columns.Count; k++)
                {
                    if (TheDataGridView.Columns[k].Visible)
                    {
                        mTempWidth += ColumnsWidth[k];
                        // If the width is bigger
                        // than the page area, then define a new
                        // column print range
                        if (mTempWidth > mTempPrintArea)
                        {
                            mTempWidth -= ColumnsWidth[k];
                            mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
                            mColumnPointsWidth.Add(mTempWidth);
                            mStartPoint = k;
                            mTempWidth = ColumnsWidth[k];
                        }
                    }
                    // Our end point is actually
                    // one index above the current index
                    mEndPoint = k + 1;
                }
            }
            // Add the last set of columns
            mColumnPoints.Add(new int[] { mStartPoint, mEndPoint });
            mColumnPointsWidth.Add(mTempWidth);
            mColumnPoint = 0;
        }
    }

    // The funtion that prints the title, page number, and the header row
    private void DrawHeader(Graphics g)
    {
        CurrentY = (float)TopMargin;

        // Printing the page number (if isWithPaging is set to true)
        if (IsWithPaging)
        {
            PageNumber++;
            string PageString = "Page " + PageNumber.ToString();

            StringFormat PageStringFormat = new StringFormat();
            PageStringFormat.Trimming = StringTrimming.Word;
            PageStringFormat.FormatFlags = StringFormatFlags.NoWrap |
                StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            PageStringFormat.Alignment = StringAlignment.Far;

            Font PageStringFont = new Font("Tahoma", 8, FontStyle.Regular,
                GraphicsUnit.Point);

            RectangleF PageStringRectangle = 
               new RectangleF((float)LeftMargin, CurrentY,
               (float)PageWidth - (float)RightMargin - (float)LeftMargin,
               g.MeasureString(PageString, PageStringFont).Height);

            g.DrawString(PageString, PageStringFont, 
               new SolidBrush(Color.Black),
               PageStringRectangle, PageStringFormat);

            CurrentY += g.MeasureString(PageString, 
                                 PageStringFont).Height;
        }

        // Printing the title (if IsWithTitle is set to true)
        if (IsWithTitle)
        {
            StringFormat TitleFormat = new StringFormat();
            TitleFormat.Trimming = StringTrimming.Word;
            TitleFormat.FormatFlags = StringFormatFlags.NoWrap |
                StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            if (IsCenterOnPage)
                TitleFormat.Alignment = StringAlignment.Center;
            else
                TitleFormat.Alignment = StringAlignment.Near;

            RectangleF TitleRectangle = 
                new RectangleF((float)LeftMargin, CurrentY,
                (float)PageWidth - (float)RightMargin - (float)LeftMargin,
                g.MeasureString(TheTitleText, TheTitleFont).Height);

            g.DrawString(TheTitleText, TheTitleFont, 
                new SolidBrush(TheTitleColor),
                TitleRectangle, TitleFormat);

            CurrentY += g.MeasureString(TheTitleText, TheTitleFont).Height;
        }

        // Calculating the starting x coordinate
        // that the printing process will start from
        float CurrentX = (float)LeftMargin;
        if (IsCenterOnPage)            
            CurrentX += (((float)PageWidth - (float)RightMargin - 
              (float)LeftMargin) - mColumnPointsWidth[mColumnPoint]) / 2.0F;

        // Setting the HeaderFore style
        Color HeaderForeColor = 
              TheDataGridView.ColumnHeadersDefaultCellStyle.ForeColor;
        if (HeaderForeColor.IsEmpty)
        // If there is no special HeaderFore style,
        // then use the default DataGridView style
            HeaderForeColor = TheDataGridView.DefaultCellStyle.ForeColor;
        SolidBrush HeaderForeBrush = new SolidBrush(HeaderForeColor);

        // Setting the HeaderBack style
        Color HeaderBackColor = 
              TheDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
        if (HeaderBackColor.IsEmpty)
        // If there is no special HeaderBack style,
        // then use the default DataGridView style
            HeaderBackColor = TheDataGridView.DefaultCellStyle.BackColor;
        SolidBrush HeaderBackBrush = new SolidBrush(HeaderBackColor);

        // Setting the LinePen that will
        // be used to draw lines and rectangles
        // (derived from the GridColor property
        // of the DataGridView control)
        Pen TheLinePen = new Pen(TheDataGridView.GridColor, 1);

        // Setting the HeaderFont style
        Font HeaderFont = TheDataGridView.ColumnHeadersDefaultCellStyle.Font;
        if (HeaderFont == null)
        // If there is no special HeaderFont style,
        // then use the default DataGridView font style
            HeaderFont = TheDataGridView.DefaultCellStyle.Font;

        // Calculating and drawing the HeaderBounds        
        RectangleF HeaderBounds = new RectangleF(CurrentX, CurrentY,
            mColumnPointsWidth[mColumnPoint], RowHeaderHeight);
        g.FillRectangle(HeaderBackBrush, HeaderBounds);

        // Setting the format that will be
        // used to print each cell of the header row
        StringFormat CellFormat = new StringFormat();
        CellFormat.Trimming = StringTrimming.Word;
        CellFormat.FormatFlags = StringFormatFlags.NoWrap |
           StringFormatFlags.LineLimit | StringFormatFlags.NoClip;

        // Printing each visible cell of the header row
        RectangleF CellBounds;
        float ColumnWidth;        
        for (int i = (int)mColumnPoints[mColumnPoint].GetValue(0);
            i < (int)mColumnPoints[mColumnPoint].GetValue(1); i++)
        {
            // If the column is not visible then ignore this iteration
            if (!TheDataGridView.Columns[i].Visible) continue; 

            ColumnWidth = ColumnsWidth[i];

            // Check the CurrentCell alignment
            // and apply it to the CellFormat
            if (TheDataGridView.ColumnHeadersDefaultCellStyle.
                       Alignment.ToString().Contains("Right"))
                CellFormat.Alignment = StringAlignment.Far;
            else if (TheDataGridView.ColumnHeadersDefaultCellStyle.
                     Alignment.ToString().Contains("Center"))
                CellFormat.Alignment = StringAlignment.Center;
            else
                CellFormat.Alignment = StringAlignment.Near;

            CellBounds = new RectangleF(CurrentX, CurrentY, 
                         ColumnWidth, RowHeaderHeight);

            // Printing the cell text
            g.DrawString(TheDataGridView.Columns[i].HeaderText, 
                         HeaderFont, HeaderForeBrush,
               CellBounds, CellFormat);

            // Drawing the cell bounds
            // Draw the cell border only if the HeaderBorderStyle is not None
            if (TheDataGridView.RowHeadersBorderStyle != 
                            DataGridViewHeaderBorderStyle.None)
                g.DrawRectangle(TheLinePen, CurrentX, CurrentY, ColumnWidth,
                    RowHeaderHeight);

            CurrentX += ColumnWidth;
        }

        CurrentY += RowHeaderHeight;
    }

    // The function that prints a bunch of rows that fit in one page
    // When it returns true, meaning that
    // there are more rows still not printed,
    // so another PagePrint action is required
    // When it returns false, meaning that all rows are printed
    // (the CureentRow parameter reaches
    // the last row of the DataGridView control)
    // and no further PagePrint action is required
    private bool DrawRows(Graphics g)
    {
        // Setting the LinePen that will be used to draw lines and rectangles
        // (derived from the GridColor property of the DataGridView control)
        Pen TheLinePen = new Pen(TheDataGridView.GridColor, 1);

        // The style parameters that will be used to print each cell
        Font RowFont;
        Color RowForeColor;
        Color RowBackColor;
        SolidBrush RowForeBrush;
        SolidBrush RowBackBrush;
        SolidBrush RowAlternatingBackBrush;

        // Setting the format that will be used to print each cell
        StringFormat CellFormat = new StringFormat();
        CellFormat.Trimming = StringTrimming.Word;
        CellFormat.FormatFlags = StringFormatFlags.NoWrap | 
                                 StringFormatFlags.LineLimit;

        // Printing each visible cell
        RectangleF RowBounds;
        float CurrentX;
        float ColumnWidth;
        while (CurrentRow < TheDataGridView.Rows.Count)
        {
            // Print the cells of the CurrentRow only if that row is visible
            if (TheDataGridView.Rows[CurrentRow].Visible)
            {
                // Setting the row font style
                RowFont = TheDataGridView.Rows[CurrentRow].DefaultCellStyle.Font;
                // If there is no special font style of the CurrentRow,
                // then use the default one associated with the DataGridView control
                if (RowFont == null)
                    RowFont = TheDataGridView.DefaultCellStyle.Font;

                // Setting the RowFore style
                RowForeColor = 
                  TheDataGridView.Rows[CurrentRow].DefaultCellStyle.ForeColor;
                // If there is no special RowFore style of the CurrentRow,
                // then use the default one associated with the DataGridView control
                if (RowForeColor.IsEmpty)
                    RowForeColor = TheDataGridView.DefaultCellStyle.ForeColor;
                RowForeBrush = new SolidBrush(RowForeColor);

                // Setting the RowBack (for even rows) and the RowAlternatingBack
                // (for odd rows) styles
                RowBackColor = 
                  TheDataGridView.Rows[CurrentRow].DefaultCellStyle.BackColor;
                // If there is no special RowBack style of the CurrentRow,
                // then use the default one associated with the DataGridView control
                if (RowBackColor.IsEmpty)
                {
                    RowBackBrush = new SolidBrush(
                          TheDataGridView.DefaultCellStyle.BackColor);
                    RowAlternatingBackBrush = new
                        SolidBrush(
                        TheDataGridView.AlternatingRowsDefaultCellStyle.BackColor);
                }
                // If there is a special RowBack style of the CurrentRow,
                // then use it for both the RowBack and the RowAlternatingBack styles
                else
                {
                    RowBackBrush = new SolidBrush(RowBackColor);
                    RowAlternatingBackBrush = new SolidBrush(RowBackColor);
                }

                // Calculating the starting x coordinate
                // that the printing process will
                // start from
                CurrentX = (float)LeftMargin;
                if (IsCenterOnPage)                    
                    CurrentX += (((float)PageWidth - (float)RightMargin -
                        (float)LeftMargin) - 
                        mColumnPointsWidth[mColumnPoint]) / 2.0F;

                // Calculating the entire CurrentRow bounds                
                RowBounds = new RectangleF(CurrentX, CurrentY,
                    mColumnPointsWidth[mColumnPoint], RowsHeight[CurrentRow]);

                // Filling the back of the CurrentRow
                if (CurrentRow % 2 == 0)
                    g.FillRectangle(RowBackBrush, RowBounds);
                else
                    g.FillRectangle(RowAlternatingBackBrush, RowBounds);

                // Printing each visible cell of the CurrentRow                
                for (int CurrentCell = (int)mColumnPoints[mColumnPoint].GetValue(0);
                    CurrentCell < (int)mColumnPoints[mColumnPoint].GetValue(1);
                    CurrentCell++)
                {
                    // If the cell belongs to invisible
                    // column, then ignore this iteration
                    if (!TheDataGridView.Columns[CurrentCell].Visible) continue;

                    // Check the CurrentCell alignment
                    // and apply it to the CellFormat
                    if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.
                            Alignment.ToString().Contains("Right"))
                        CellFormat.Alignment = StringAlignment.Far;
                    else if (TheDataGridView.Columns[CurrentCell].DefaultCellStyle.
                            Alignment.ToString().Contains("Center"))
                        CellFormat.Alignment = StringAlignment.Center;
                    else
                        CellFormat.Alignment = StringAlignment.Near;
                    
                    ColumnWidth = ColumnsWidth[CurrentCell];
                    RectangleF CellBounds = new RectangleF(CurrentX, CurrentY,
                        ColumnWidth, RowsHeight[CurrentRow]);

                    // Printing the cell text
                    g.DrawString(
                      TheDataGridView.Rows[CurrentRow].Cells[CurrentCell].
                      EditedFormattedValue.ToString(), RowFont, RowForeBrush,
                      CellBounds, CellFormat);
                    
                    // Drawing the cell bounds
                    // Draw the cell border only
                    // if the CellBorderStyle is not None
                    if (TheDataGridView.CellBorderStyle != 
                                DataGridViewCellBorderStyle.None)
                        g.DrawRectangle(TheLinePen, CurrentX, CurrentY, 
                              ColumnWidth, RowsHeight[CurrentRow]);

                    CurrentX += ColumnWidth;
                }
                CurrentY += RowsHeight[CurrentRow];

                // Checking if the CurrentY exceeds the page boundaries
                // If so, then exit the function and returning true meaning another
                // PagePrint action is required
                if ((int)CurrentY > (PageHeight - TopMargin - BottomMargin))
                {
                    CurrentRow++;
                    return true;
                }
            }
            CurrentRow++;
        }

        CurrentRow = 0;
        // Continue to print the next group of columns
        mColumnPoint++;

        if (mColumnPoint == mColumnPoints.Count)
        // Which means all columns are printed
        {
            mColumnPoint = 0;
            return false;
        }
        else
            return true;
    }

    // The method that calls all other functions
    public bool DrawDataGridView(Graphics g)
    {
        try
        {
            Calculate(g);
            DrawHeader(g);
            bool bContinue = DrawRows(g);
            return bContinue;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Operation failed: " + ex.Message.ToString(),
                Application.ProductName + " - Error", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            return false;
        }
    }
}

该类的问题

  • 如果特定列的宽度大于页面宽度,则排除的文本将不会被换行或打印在另一页中。
  • 该类不支持图像单元格。

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.