本文介绍了如何在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=共享

推荐答案

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

它打印 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.

也是一个 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 控件用于提供更多选项.

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的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:35