Re: Printing DBGrid with Delphi

Posted by webmaster Guido on February 21, 2003

In Reply to: Printing posted by Ste D on February 20, 2003

: is there any way to print just the dbgrid on a form with all the fields and values just at the press of a button?? there are other things on the form but i dont wont these printing, just the table with ALL the information stored in it.

There is no simple command to print the contents of a Delphi DBGrid or a complete table. So you either write the entire code for this yourself, or you use a report generator to print out data from your table (such as QuickReport which is included with Delphi), or install a component that can print a DBGrid. There are some freeware as well as shareware components for this; some suggested keywords for the search engines: delphi dbgrid print component.

Writing the entire code yourself can range from not-do-difficult to quite complicated... Printing some records in a fixed font like "Courier New", not filling more then one page, is not so hard. But it's another story when it comes to using variable width fonts, printouts that cover several pages, incuding headers, footers, page numbers, lines and so on. An example of what the code might look like, can be found here:

...and this example only prints some constant demo lines, so you still have to add the part that extracts data from the table and feeds the data to the printing code.

While there is some nice code around that gives you a good idea how printing works (and how complicated it can get), it's very time consuming to adapt it and set it up for printing database stuff in a tabular form. Personally I always use Delphi's QuickReport, that's quick and painless :)

A temporary quick and dirty solution:

With Form1.Print you can print a graphical image of the entire form. That limits the number of records that are printed to the data that are visible in the DBGrid, and of course all the other components of the form are printed too. So here's an idea for a work around:

Put a copy of the DBGrid on its own form (say Form2), entirely filling Form2. When the print-button is clicked, show Form2, synchronize the grid on Form2 with the original grid, do a Form2.Print and finally hide Form2 again ;-)

Find related articles

Search for:  DBGrid   Print   Table

Delphi Forum :: Tutorials :: Source code :: Tips