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

打印DataGrid中的选定列和行

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (20投票s)

2006 年 11 月 30 日

2分钟阅读

viewsIcon

135332

downloadIcon

12269

一篇关于在 DataGrid 中打印选定列和行的文章。

引言

现有的关于 DataGrid 打印的文章只能打印所有列和行。有时用户需要打印 DataGrid 中的特定列和行。以下是一些示例:

  • DataGrid 中有大量的行,不需要全部打印。
  • 列宽的总和可能大于页面宽度,最好在打印时删除一列或多列。
  • 用户想要过滤 DataGrid 内容,然后打印过滤后的数据。

因此,实现了一个名为 PrintDG 的类,可以在应用程序中使用。我已经在 C# 和 VB.NET 中为 DataGridDataGridView 做了这件事。

描述

代码的主要部分是 PrintDG 类。

PrintDG 中,我们有:

  • SelectedColumnsAvailableColumns 列表用于保存列名。
  • 一个名为 PrintDocPrintDocument 对象(带有 BeginPrintPrintPage 事件处理程序)。
  • 四个函数:
    • Print_DataGrid:可以从类外部调用的主函数。
    • PrintDoc_BeginPrint:初始化一些变量以开始打印。
    • PrintDoc_PrintPage:执行打印作业。
    • DrawFooter:写入页码。

使用代码

要使用 PrintDG 类,DataGrid 必须有一个 TableSyledg.TableStyles[0]。在主窗体中,DataGrid 填充了 'Northwind.mdb' 的 'Order' 表,并为 DataGrid 定义了一个 TableStyle。在 PrintOption 窗体中,用户可以选择 DataGrid 列、字体、前景色和标题。

以下是 PrintDoc_PrintPage 事件处理程序中的一段代码。它执行以下任务:

  • 循环访问 DataView 中的所有行。
  • 在“打印选定行”模式下,如果未选择当前行,则忽略它。
  • 如果到达页面末尾,则写入页码并转到下一页。 如果未到达页面末尾,则:
  • 如果是新页面,则绘制标题和列(它会检查是否用户选择了每列,如果未选择,则跳过它)。
  • 绘制 TextBoxColumnBoolColumn 的列内容(它会检查是否用户选择了每列,如果未选择,则跳过它)。
  • 绘制边框。
  • 计算第一页的“每页行数”。
// Print Current Page, Row by Row 
while (RowPos <= dv.Count - 1)
{
    if (!PrintAllRows && !dg.IsSelected(RowPos))
    {
        RowPos++;
        continue;
    }
    DataRowView oRow = dv[RowPos];
    if (nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
    {
        DrawFooter(e, RowsPerPage);
        NewPage = true;
        PageNo++;
        e.HasMorePages = true;
        return;
    }
    else
    {
        if (NewPage)
        {
            // Draw Header
            e.Graphics.DrawString(PrintTitle, 
                    new Font(PrintFont, FontStyle.Bold), 
                    Brushes.Black, e.MarginBounds.Left, 
                    e.MarginBounds.Top -
                    e.Graphics.MeasureString(PrintTitle, 
                    new Font(PrintFont, FontStyle.Bold), 
                    e.MarginBounds.Width).Height - 13);

            string s = DateTime.Now.ToLongDateString() + " " + 
                       DateTime.Now.ToShortTimeString();
            e.Graphics.DrawString(s, new Font(PrintFont, 
                    FontStyle.Bold), Brushes.Black, 
                    e.MarginBounds.Left + (e.MarginBounds.Width - 
                    e.Graphics.MeasureString(s, 
                    new Font(PrintFont, FontStyle.Bold), 
                    e.MarginBounds.Width).Width), e.MarginBounds.Top - 
                    e.Graphics.MeasureString(PrintTitle, 
                    new Font(new Font(PrintFont, FontStyle.Bold), 
                    FontStyle.Bold),
                    e.MarginBounds.Width).Height - 13);

            // Draw Columns
            nTop = e.MarginBounds.Top;
            i = 0;
            foreach (DataGridColumnStyle GridCol in 
                     dg.TableStyles[0].GridColumnStyles)
            {
                if (!PrintDG.SelectedColumns.Contains(GridCol.HeaderText))
                    continue;
                e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), 
                        new Rectangle((int) ColumnLefts[i], 
                        nTop, (int) ColumnWidths[i], nHeight));
                e.Graphics.DrawRectangle(Pens.Black, 
                         new Rectangle((int) ColumnLefts[i], nTop,
                        (int) ColumnWidths[i], nHeight));
                e.Graphics.DrawString(GridCol.HeaderText, PrintFont, 
                        new SolidBrush(PrintFontColor), 
                        new RectangleF((int) ColumnLefts[i],nTop, 
                        (int) ColumnWidths[i], nHeight), StrFormat);

                i++;
            }
            NewPage = false;
        }
        nTop += nHeight;
        i = 0;
        // Draw Columns Contents
        foreach (DataGridColumnStyle oColumn in 
                 dg.TableStyles[0].GridColumnStyles)
        {
            if (!PrintDG.SelectedColumns.Contains(oColumn.HeaderText))
                continue;
            string cellval = oRow.Row[oColumn.MappingName].ToString().Trim();
            if (ColumnTypes[i].ToString() == 
               "System.Windows.Forms.DataGridTextBoxColumn")
            {
                // For the TextBox Column
                // Draw Content of TextBox Cell
                e.Graphics.DrawString(cellval, PrintFont, 
                        new SolidBrush(PrintFontColor),
                        new RectangleF((int) ColumnLefts[i], nTop, 
                        (int) ColumnWidths[i], nHeight), StrFormat);
            }
            else if (ColumnTypes[i].ToString() == 
                     "System.Windows.Forms.DataGridBoolColumn")
            {
                // For the CheckBox Column
                // Draw Content of CheckBox Cell
                ChkBox.Size = new Size(14, 14);
                ChkBox.Checked = (bool) (oRow.Row[oColumn.MappingName]);
                Bitmap oBitmap = new Bitmap((int) ColumnWidths[i], nHeight);
                Graphics oTempGraphics = Graphics.FromImage(oBitmap);
                oTempGraphics.FillRectangle(Brushes.White, 
                   new Rectangle(0, 0, oBitmap.Width, oBitmap.Height));
                ChkBox.DrawToBitmap(oBitmap, 
                        new Rectangle((int)((oBitmap.Width - ChkBox.Width) / 2), 
                        (int) ((oBitmap.Height - ChkBox.Height) / 2), 
                        ChkBox.Width, ChkBox.Height));
                e.Graphics.DrawImage(oBitmap, 
                  new Point((int) ColumnLefts[i], nTop));
            }
            e.Graphics.DrawRectangle(Pens.Black, 
                       new Rectangle((int) ColumnLefts[i], 
                       nTop, (int) ColumnWidths[i], nHeight));
            i++;
        }
    }
    RowPos++;
    // For the first page it calculates Rows per Page
    if (PageNo == 1) RowsPerPage++;
}

历史

  • 2006 年 12 月 12 日:修复了 Bug - 当没有选定的行时,尝试设置“要打印的行”=“选定的行”可能会导致“除以零”错误消息。(感谢 khanhdl。)
© . All rights reserved.