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?
Thanks in advance!
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?
Thanks in advance!
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.
.NET WinForm controls (like the datagridview) do not natively support being printed.
In the case of the datagridview, though, you can call 'DrawToBitmap', and then take that bitmap and pass it on to the printing functions in order to produce it on paper.
There are projects on CodeProject that have done some work printing DataGridViews.
You could export the DataGridView to Excel and then print it from Excel.
You could also consider to not show your data in a DataGridView, but show it in a ReportViewer control, which has the ability to export to PDF or Excel. From there it's possible to print your data.
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).
i can help you tina... open c# project file (new), add a datagridview,one printdocument, one button
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);
}
thats all your data printing:D
i can help all of you if you want everyone;)my msn is vagabondage[at]windowslive.com
I know you've already accepted an answer, but for the next person to search this question...
I also found this wonderful project on Code Project, and just implemented it. It was EASY and nice. http://www.codeproject.com/KB/grid/GridDrawer.Net.aspx