-5

我有一个具有 sqlquery 数据的 datagirdview。我想打印这个。请告诉我打印按钮的代码。我正在使用 C#

在此处输入图像描述

4

3 回答 3

1

这是关于它的文章DataGridViewPrinter 类。您可以使用此类轻松打印 DataGridView。

例如,您可以在 Toolbox 中的表单上添加PrintDocument 组件,并在其 PrintPage 事件中编写以下代码:

 bool more = printer.DrawDataGridView(e.Graphics);
 if (more == true)
     e.HasMorePages = true;

其中打印机是 DataGridViewPrinter 对象。

要打印此文档,您可以添加按钮并将此代码添加到它的单击事件中:

        printer = new DataGridViewPrinter(yourGridView, printDocument1, 
                      true, true, "title", this.Font, Color.Black, true);

        if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            printDocument1.Print();
        }
于 2013-02-23T09:28:23.790 回答
1

这是一个很好的例子,您可以使用如何打印数据网格

这是一些代码:

private void btnPrint_Click(object sender, EventArgs e)
{           
    //Open the print dialog
    PrintDialog printDialog = new PrintDialog();            
    printDialog.Document = printDocument1;
    printDialog.UseEXDialog = true;    
    //Get the document
    if (DialogResult.OK == printDialog.ShowDialog())
    {
        printDocument1.DocumentName = "Test Page Print";                
        printDocument1.Print();
    }
    /*
    Note: In case you want to show the Print Preview Dialog instead of 
    Print Dialog then comment the above code and uncomment the following code
    */

    //Open the print preview dialog
    //PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
    //objPPdialog.Document = printDocument1;
    //objPPdialog.ShowDialog();
}
于 2013-02-23T09:29:26.280 回答
1

检查此代码可能对您有帮助:-

 using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Printing;

    public class Form1 :
        Form
    {
        private Button printButton = new Button();
        private PrintDocument printDocument1 = new PrintDocument();

        public Form1()
        {
            printButton.Text = "Print Form";
            printButton.Click += new EventHandler(printButton_Click);
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            this.Controls.Add(printButton);
        }

        void printButton_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }


        Bitmap memoryImage;

        private void CaptureScreen()
        {
            Graphics myGraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
        }

        private void printDocument1_PrintPage(System.Object sender,  
               System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }



        public static void Main()
        {
            Application.Run(new Form1());
        }
    }
于 2013-02-23T09:22:35.940 回答