【问题标题】:How to print the values of datagridview in c#? [duplicate]如何在 C# 中打印 datagridview 的值? [复制]
【发布时间】:2014-12-12 17:23:10
【问题描述】:

我想打印 DGV 的整行。

这应该通过提取不拍照的值来完成...(如果您不明白,请在下面评论)

有什么办法可以做到吗?

仍然面临问题... https://drive.google.com/file/d/0BxqCNfHpYEJlUVg3VW5aZlpHMlk/view?usp=sharing

【问题讨论】:

  • 请不要只要求我们为您解决问题。向我们展示如何尝试自己解决问题,然后向我们确切地展示结果是什么,并告诉我们您为什么觉得它不起作用。请参阅“What Have You Tried?”了解您真正需要阅读的优秀文章。你知道如何打印吗?任何事物?你将如何打印“你好,世界”?

标签: c# winforms printing datagridview


【解决方案1】:

这是一个绝对最小的代码示例。

它打印 dgv 的所有行,包括简单的页眉和页脚..

您需要在表单中添加PrintDocument printDocument1

也是Button cb_printPreview

// a few variables to keep track of things across pages:
int nextPageToPrint = -1;
int linesOnPage = -1;
int linesPrinted = -1;
int maxLinesOnPage = -1;

private void printDocument1_PrintPage(object sender, 
                                      System.Drawing.Printing.PrintPageEventArgs e)
{
    // for each fresh page:
    linesOnPage = 0;
    nextPageToPrint++;

    // a short reference to the DataGridView we want to print
    DataGridView DGV = yourDataGridView;

    // I prefer mm, pick your own unit!
    e.Graphics.PageUnit = GraphicsUnit.Millimeter;

    // I want to print the columns at these fixed positions
    // the first one is the left margin, 
    // the last one a dummy at the right page margin
    int[] tabStops = new int[4] {15, 30, 100, 190};

    // I want only one column to be right-aligned:
    List<int> rightAlignedCols = new List<int>() { 1 };

    // we need to keep track of the horizontal position
    // this is also the top margin:
    float y = 35f;
    // we add a little space between the lines:
    float leading = 1.5f;
    // we will need to measure the texts we print:
    SizeF size = Size.Empty;
    // we use only one font:
    using (Font font = new Font("Consolas", 13f))
    {
        // a simple header:
        e.Graphics.DrawString("List " + printDocument1.DocumentName, 
                               font, Brushes.Black, 50, y);
        y += size.Height + 15;

        // I loop over the all rows:
        for (int row = linesPrinted; row < DGV.Rows.Count; row++)
        {

            // I print a light gray bar over every second line:
            if (row % 2 == 0) 
                e.Graphics.FillRectangle(Brushes.Gainsboro, 
                tabStops[0], y - leading / 2, e.PageBounds.Width - 25, size.Height);

            // I print all (3) columns in black, unless they're empty:
            for (int col = 0; col < DGV.ColumnCount; col++)
                if (DGV[0, row].Value != null)
                {
                    string text = DGV[col, row].FormattedValue.ToString();
                    size = e.Graphics.MeasureString(text, font, 9999);
                    float x = tabStops[col];
                    if (rightAlignedCols.Contains(col) ) 
                        x = tabStops[col + 1] - size.Width;
                    // finally we print an item:
                    e.Graphics.DrawString(text, font, Brushes.Black, x, y);
                }
           // advance to next line:
            y += size.Height + leading;
            linesOnPage++;
            linesPrinted++;
            if (linesOnPage > maxLinesOnPage)   // page is full
            {
                e.HasMorePages = true;      // more to come
                break;                      // leave the rows loop! 
            }         
       }
       e.Graphics.DrawString("Page " + nextPageToPrint, font, 
                               Brushes.Black, tabStops[3] -20, y + 15);

    }
}

private void cb_printPreview_Click(object sender, EventArgs e)
{
    PrintPreviewDialog PPVdlg = new PrintPreviewDialog();
    PPVdlg.Document = printDocument1;
    PPVdlg.ShowDialog();
}

private void printDocument1_BeginPrint(object sender, 
                                       System.Drawing.Printing.PrintEventArgs e)
{
    // create a demo title for the page header:
    printDocument1.DocumentName = " Suppliers as of  " 
                                  + DateTime.Now.ToShortDateString();
    // initial values for a print job:
    nextPageToPrint = 0;
    linesOnPage = 0;
    maxLinesOnPage = 30;
    linesPrinted = 0;
}

可以从PrintPreviewDialog触发实际打印。

还有一个PrintDialog 和一个PrintPreview 控件可提供更多选项。

显然可以添加更多东西,包括图形、多种字体等,但这应该会给你一个想法。完整的教程请参阅 WWW 和 MSDN!

【讨论】:

  • 谢谢你,TaW...但仍然面临问题...drive.google.com/file/d/0BxqCNfHpYEJlUVg3VW5aZlpHMlk/… 如果这看起来很明显,请多多包涵...
  • 哈哈! (对不起,-)请尝试不仅要复制而且还要理解代码!我敢打赌,您的 dgv 中的行数少于 30 行?更改开始和/或结束条件以适合您的情况。您甚至可以遍历某些选定行的 List 或您想要顶部打印的任何其他行集合..
  • 你在线吗?我的电子邮件 id 是 senthilvivek93@gmail.com 请我们聊天吗?
  • 不是真的,我得走了。对不起! (顺便说一句,你会建议什么聊天媒体,我认为你不能使用 SO 聊天,对吗?) - 我会在几个小时后回来..
  • 任何必要的...团队查看器或谷歌视频群聊...(这就是我给你我的邮件ID的原因)...请不要挂断我的电话...我需要完成这个...我已经过了最后期限...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2010-10-19
相关资源
最近更新 更多