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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:WinForms does provide a standard printing system, which you can harness to print out the contents of your
DataGridViewcontrol. You’ll want to use thePrintDocumentclass, 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.You could export the data from your
DataGridViewto Microsoft Excel, and then print it from there. Excel has much more robust, built-in printing support.If you’re not interested in rolling your own solution, you can browse CodeProject for some already designed solutions. For example:
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.
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
DrawToBitmapmethod exposed by every control. This is a really quick-and-dirty approach that draws an exact image of theDataGridViewcontrol as it appears on your screen to aBitmap, which you can then pass directly to your printer.