Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XmlStore Part 2: Printing DataGridView Contents

0.00/5 (No votes)
18 Mar 2007 1  
A utility to read, edit, encrypt, decrypt, write and print XmlStore files
Screenshot - XmlStore2.png

Introduction

An earlier article A Utility to Read, Edit, Encrypt, Decrypt, Write XmlStore Files is part 1 of a project on using a DataGridView as the UI for a simple XML file. Here's an excerpt from its introduction:

XmlStore is intended to be a "no brainer" format.

XmlStoreClient is a WinForm client with a simple user interface. It displays the contents of any XmlStore file in a DataGridView. You can navigate to any record (row) or field (column), edit it, and save your changes. You can also encrypt and decrypt

  • an individual cell
  • an entire row
  • an entire column, or
  • the entire data set (table)

Part 1 describes many of the pertinent details and they are not repeated here. However, Part 1 does not address printing the contents of an XmlStore. This article does. The code provided with this article does everything Part 1 does; it also supports printing on any Windows supported printer.

WARNING

This utility supports encryption. If you encrypt anything which requires you to supply a password, you will need the same password to decrypt the data. YOU have to remember it, as the password used for encryption is not stored anywhere.

Background

Situation

On completing my XmlStore project (described in part 1), I discovered I had additional uses for the XmlStore project. However, for some of them, I needed an easy way to print the contents of the XmlStore. Nothing fancy, just "reasonably nice looking", "consistent", and "predictable".

It turns out, printing a people-friendly version of a DataGridView (which in this case is really the content of an XmlStore file) is not supported by Microsoft. It looked, at first, like an oversight on that company's part. Then I thought printing must be really easy. Perhaps all it required was to write a simple PrintDGV method and tally ho!

Complication

Was I wrong!!! Sadly, printing anything in Microsoft Windows using C# is a complicated event-driven process. Simply put, one has to render each page when asked by the printer driver. But, for a newbie like myself, comprehending the convoluted MSDN documentation on printing was, and still is, very difficult.

So, I searched around The Code Project to see if someone had a solution that would meet my needs. To my great relief, more than a few had contributed classes that promised a useable solution. (Isn't The Code Project community an amazing resource?) The noteworthy ones I found are:

Each is an interesting solution but, unfortunately, I couldn't use any one of them "out of the box". I realized, I'd have to tweak each one to get what I needed. The tweaks included, among other things, the ability to:

  • switch printers
  • change orientation
  • display a custom title in the top margin
  • print the page number in the bottom margin

Indeed, if the tweaks weren't necessary, this article would have been moot.

Solution

To explore, tweak, and finally pick, the solution that would work best (for me), I created a class called Print in my VVX namespace. VVX.Print is essentially a wrapper class that provides access to modified versions of the three classes mentioned above. I then added the following menuitems to the File menu:

  • Print How determines if Everything (default) or only Selected Columns of a DataGridView control's contents are previewed and printed.
  • Print Preview displays the standard Windows print preview window shown in the picture above. You can then either close that window or proceed to print a hard copy [by clicking on its printer icon].
  • Print ... prints the page without a chance to preview.
  • Print Screenshot lets you preview and print a screen shot of the entire form with all the controls in it.
  • Print DataGridView Control lets you preview and print only the DataGridView control, just as it appears on the screen.

NOTE: When appropriate, each one also displays the standard print dialog with which you can change printers, change orientation, etc. Unless you're a Windows veteran, a potentially confusing thing [in the context of Print Previews] is: the PrintDialog displays a "Print" button. It should have been called "OK" or "Apply" or "Continue", because that's what it really does. As I don't know how to fix this, I hope this note suffices.

Using the Code

DISCLAIMER

I undertook this project originally to help a friend and, in the process, also to learn a few things about Visual Studio 2005, C#, XML, DataGridView, cryptography, and printing, etc. Caveat Emptor: Nothing in my code has been properly or adequately tested. More important, there is a good chance, you or someone else can do this better. So, if you do use this code and cannot decrypt something, there is little I or anyone can do to help you. On the bright side, though, since you have the source code, you can make it more bullet-proof to suit your needs. Also, printing is full of in-the-eyes-of-the-beholder issues and you just might hate how they are addressed here!

Building the Utility

If you have Visual Studio 2005, then you should be able to use the project source code "out of the box" -- simply build and run. The code itself is not rocket science. It is reasonably documented. If you don't have Visual Studio 2005, you will have to ask a more experienced friend. (Don't ask me, as I don't have a clue! Don't ask Microsoft either, as no one there will respond!!!)

Code Modules

Side benefits of the code for newbies (like myself): The project includes modules with reasonably documented and self-explanatory code. With luck, they may help you learn how to use a few features of the various technologies employed. Most of the things that merit mention, such as XmlStore and the various VVX modules, are described in part 1. The following are mentioned below mainly for the printing related modules.

VVX_Print.cs

VVX.Print is essentially a wrapper class that provides access to modified versions of three classes:

These modified versions are placed in my VVX namespace so that I can freely tinker with the code.

The VVX.Print class contains one enum, two constructors, three public methods, and a handful of properties.

VVX.Print Class Methods

The VVX.Print class contains three simple public methods:

  • DoPrintDGV prints the contents of a DataGridView
  • DoPrintControl prints any control, including the entire Form itself
  • DoGetPrinterAndSettingsFromUser displays the PrintDialog and gets the settings/preferences from the user. Essentially it updates the System.Drawing.Printing.PrinterSettings of the System.Drawing.Printing.PrintDocument instance employed.

VVX.Print Class Properties

The VVX.Print class contains a few simple public properties:

  • PrintWhichDGV identifies the DataGridView to be printed.
  • PrintHowDGV determines how the DataGridView will be printed (e.g., Everything or Selected Columns).
  • PrintPreview determines if the print preview window will be displayed.
  • PrintTitle is the text displayed in the top margin of the page. The bottom margin displays the page number, by default. As used in this project, the file name [sans the path] is set as the Title.
  • PrintJobName is the name of the print job. It will be listed in one or more of the following:
    • Your computer's Printer Status window,
    • Possibly in the printer's display panel, as in some HP printers,
    • On your computer when the print job is "done"; depending on your version of Windows, it may be displayed in a little pop-up box like the one shown below:

    Print Status

My Thanks to the People who Helped me in this Project

Salan Al-Ani, Afrasiab Cheraghi, and Nader Elshehabi collectively did much to improve my understanding of not only how to print data from a DataGridView, but also about printing in Windows.

Improvements Needed

There is plenty of room for improving this utility, such as the ability to do some or all of the following:

  • "test" and "repair" an XmlStore file
  • Create a new XmlStore file
  • Change the Crypto password in one simple step
  • Rearrange the columns and/or rows in the DataGridView
  • Create a PDF file (instead of a printed copy) from the data in the DataGridView
  • Plot a chart of the data in the XmlStore file
  • ...

History

  • 18th March, 2007: Initial post

My Other Recent Contributions

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here