Printing a .NET DataGridView

后端 未结 9 775
南旧
南旧 2020-12-08 05:33

I am fairly new to .NET and C#, but I have a DataGridView that I would like to print. What would be the best way to go about doing so?

相关标签:
9条回答
  • 2020-12-08 06:12

    Add a DataGridView, a PrintDocuemnt, and a Button then:

    button click events {
        printDocument1.Print();
    }
    
    printDocument1_PrintPage events {
        Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
        this.dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
        e.Graphics.DrawImage(bm, 0, 0);
    }
    

    That's all your data printing.

    0 讨论(0)
  • 2020-12-08 06:12

    There is no built-in print support I'm afraid.

    You might resort to using a third party control such as the Infragistics WinGrid which has build-in support for printing.

    0 讨论(0)
  • 2020-12-08 06:19

    If you are going to be printing more than just DataGridViews, then a more generic approach may be worth pursuing. We use MigraDoc and then wrote our own class to read DataGridViews and output MigraDoc classes representing a table.

    There are lots of great printing packages available, but I only have experience with MigraDoc.

    Edit:

    In response to comments, here is a link to my site showing the code I created for generating MigraDoc tables and an example of using it to display a DataGridView (DataGridView to MigraDoc tables).

    0 讨论(0)
提交回复
热议问题