如何打印在C#DataGridView的价值? [英] How to print the values of datagridview in c#?

查看:83
本文介绍了如何打印在C#DataGridView的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个DGV的整个行。

i want to print entire rows of an DGV.

这应该通过提取值不拍照做...(如果你不明白请评论如下)

this should be done by extracting the values not taking picture...(if you don't understand please comment below)

有什么办法,我可以做到这一点?

is there any way i can do this?

仍然面临的问题...
https://drive.google.com/file/d/0BxqCNfHpYEJlUVg3VW5aZlpHMlk/view ?USP =共享

Still facing problem... https://drive.google.com/file/d/0BxqCNfHpYEJlUVg3VW5aZlpHMlk/view?usp=sharing

推荐答案

下面是一个绝对最低 code的例子。

Here is an absolutely minimal code example.

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

It prints all rows of a dgv, including simple headers and footers..

您需要一个的PrintDocument printDocument1 添加到您的表单。

You need to add a PrintDocument printDocument1 to your form.

另外一个按钮cb_print preVIEW

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

在实际印刷可以从打印$ P $触发pviewDialog

还有一个 PrintDialog类打印preVIEW 查看更多选项的控制。

There is also a PrintDialog and a PrintPreview control for more options.

显然可以增加很多事情,包括图形,多种字体等,但是这应该给你一个想法。对于全教程请看参考WWW和放大器; MSDN!

Obviously one can add many more things, including graphics, multiple fonts etc, but this should give you an idea.. For full tutorials please look refer to the WWW & MSDN!

这篇关于如何打印在C#DataGridView的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆