Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print data from a DataGridView in C#?

I am developing a program, and I need to print the Bills data that is displayed in a DataGridView control.
I want to know the code how to print the data in the DataGridView.

I am using Visual Studio 2008 and C# 3.5

like image 766
Omneya Avatar asked Dec 25 '10 11:12

Omneya


People also ask

How do I print from datagrid?

You can print the content of SfDataGrid using PDF exporting and PrintDialog support. For that, export the SfDataGrid to PDF and load the exported PDF document into PdfDocumentView. Then, print the document in PdfDocumentView using the PrintDialog. Document.

How retrieve data from database and display in DataGridView in C#?

Step 1: Make a database with a table in SQL Server. Step 2: Create a Windows Application and add DataGridView on the Form. Now add a DataGridView control to the form by selecting it from Toolbox and set properties according to your needs.

What is Dgvprinter C#?

This class includes codes to print Bill from data grid view in C# windows form application. It is a part of my Tutorial Series on "How to Create Billing System in C#"


1 Answers

There isn't any printing support built into the DataGridView, so you'll have to implement this yourself. There are a couple of possible solutions:

  1. WinForms does provide a standard printing system, which you can harness to print out the contents of your DataGridView control. You'll want to use the PrintDocument class, so the relevant documentation is a great place to start reading. The advantage of this method is that it allows you complete control over the format and layout of the printed document.

  2. You could export the data from your DataGridView to Microsoft Excel, and then print it from there. Excel has much more robust, built-in printing support.

  3. If you're not interested in rolling your own solution, you can browse CodeProject for some already designed solutions. For example:

    • The DataGridViewPrinter Class
    • Printing a DataGridView on DotNet Framework
    • Another DataGridView Printer
    • DataGridView Printing by Selecting Columns and Rows

    Even if you don't find a drop-in solution that fits your exact needs, you can probably get a good idea of how to go about creating this functionality yourself by using the published code as an example.

  4. If you're up for a really hacky solution (and you don't have any desire or need to customize the layout or design of the printed output), you could use the DrawToBitmap method exposed by every control. This is a really quick-and-dirty approach that draws an exact image of the DataGridView control as it appears on your screen to a Bitmap, which you can then pass directly to your printer.

like image 108
Cody Gray Avatar answered Oct 07 '22 04:10

Cody Gray