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?
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.
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.
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).