Tina Orooji
Tina Orooji

Reputation: 1840

Printing a .NET DataGridView

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?

Upvotes: 15

Views: 38717

Answers (9)

mehmet
mehmet

Reputation:

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.

Upvotes: 13

user153923
user153923

Reputation:

On CodeProject:

Another DataGridView Printer by aureolin

Above is another excellent DataGridView Printer that is extremely simple to implement.

I realize I am a Johnny Come Lately to this thread. I stumbled upon it while searching for something else, but wanted to reference this excellent printing solution for others who happen by this way (as I did).

I hope someone here gets some use out of it.

Upvotes: 5

tiger555
tiger555

Reputation: 11

I like this method http://www.codeproject.com/KB/grid/GridDrawer.Net.aspx But is there any way I can choose the printer.

I want to auto print at the end of a process. So planing to choose this as options 1, so i can always print to default printer. And another option like to choose the printer and then print. So in total 2 ptint function in all. Please reply. Cheers

Upvotes: 1

David
David

Reputation: 73554

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

Upvotes: 5

Robert Gowland
Robert Gowland

Reputation: 7947

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

Upvotes: 1

Bliek
Bliek

Reputation: 466

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.

Upvotes: 3

Bob
Bob

Reputation: 99674

There are projects on CodeProject that have done some work printing DataGridViews.

Upvotes: 8

GWLlosa
GWLlosa

Reputation: 24403

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

Upvotes: 2

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

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.

Upvotes: 3

Related Questions