-5

私はsqlqueryデータを持つdatagirdviewを持っています。これを印刷したい。印刷ボタンのコードを教えてください。私はC#を使用しています

ここに画像の説明を入力

4

3 に答える 3

1

これに関する記事はThe DataGridViewPrinter Classです。このクラスを使用して、DataGridView を簡単に印刷できます。

たとえば、ツールボックスからフォームにPrintDocument コンポーネントを追加し、その PrintPage イベント内に次のように記述できます。

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

ここで、printer は 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 に答える