Print DataGridView

Nemesis09

Active member
Joined
Jul 25, 2006
Messages
31
Location
NSW, Australia
Programming Experience
Beginner
Hi,
I have a DataGridView, which is already filled (manually via
Grid.Rows(y).Cells(x).Value = "") and I need to be able to print the datagridview. Idealy in a simmilar style to the datagridview, ie. the column headings in bold or something at the top of each column, and the whole grid (currently 5 columns) automatically fit to the width of the page.

A print dialogue would also be cool, but I can live without.

Thanks in advance for any help.
 
Printing is printing in .NET. You create a PrintDocument and call its Print method, then handle its PrintPage event. There is a printing example in the 2003 101 Samples from Microsoft. It will most likely just print some text, but everything else is done the same way. In the PrintPage event handler you call the appropriate methods of the e.Graphics object to draw whatever you want on the printed page.

If you don't want to do it all yourself the good news is that someone else has already done it. The following link is to a utility for printing a DataGrid, but someone has posted a similar utility for the DataGridView in post #10.

http://www.vbforums.com/showthread.php?t=356115
 
Thanks for the info... *blinks in utter confusion*:(

I tried the DataGridViewPrinter from the thread you pointed me to, but I cant even get it to compile, let alone work. Although it looks like its exactly what I need (Well, the DataGrid one is at least, I assume the DataGridView is the same).

I've added the Class "DataGridViewPrinter", added a PrintDocument called "MyPrintDocument" and when I try to run/debug it heres what I get:


1. Type "Margins" is not defined

2. Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

3. Name 'mydatagridviewprinter' is not declared.

4. Name 'mydatagridviewprinter' is not declared.

5. Name 'MyDataGridViewPrinter' is not declared.

6. Name 'MyConstants' is not declared.


Once again, any help will be greatly appriciated.
Thanks.
 
Those issues are all quite simple to solve.

1. Go to the MSDN library and search for "margins class". You'll quickly find its help topic which tells you that it's a member of the System.Drawing.Printing namespace, so import the namespace or qualify the class name.

2. Thats' a very standard issue with converted C# or VB.NET 2003 code. Simply qualify DialogResult with Windows.Forms to specify the System.Windows.Forms.DialogResult type rather than the Me.DialogResult property.

3, 4 & 5. Declare a class-level variable named MyDataGridViewPrinter, which you would have seen done in the original code if you had investigated that far.

6. Have a look at the line that uses MyConstants. It is displaying a MessageBox when an error occurs. Obviously the original author has declared a standard caption to use for error messages. Just use something else.

The moral of the story: you should investigate rather than take things on face value. Just because someone gives you some code is no guarantee that it will do what you want. You need to study the code and gain an understanding of it to make sure it is doing what you want. Also, MSDN should always be your first stop when you have a question.
 
...oh

ok, ok, I got it to work... sorry...

I did see that most of my problems seemed easyfixed (ie declaring the missing variable and just changeing the msgbox to "Error" for the time being) but I assumed that this was too obvious to work, and I still had other problems anyway. So I put it all back to the way it was and gave you the full error report word for word from the error window... I thought you'd have a better chance of fixing the problem without having to deal with my "...maybe this will work..." solutions in place.

But thanks heaps for not insulting me to violently...
I'm pretty new to the whole .NET scene (ie. this is my first app in .net) so thanks again for your quick, accurate and effective answers and advice.
 
I've just got one more question, is there any way I can change the width of the columns?

As my grid has only 5 columns which only have short strings in them, when it prints, the whole grid is reduced to a tiny square in the centre of the screen.

Idealy, instead of the columns being the width of the longest string in the column I'd like them each to be "(pagewidth-margins)/number_of_columns" wide.

...I hope this isn't such a stupid question.
 
It's not a stupid question and it's quite possible, but it's up to you to change the way the class works. If you want the columns to fill the page by default then it's up to you to measure the page and determine what the dewfault width of a column should be. If any columns are wider than that then you'll need to reduce the widths of the other columns to compensate. It's purely a matter of mathematics. Think about how you would accomplish the task yourself if you had fixed-width strings to write on a piece of paper and you'll find that it's done exactly the same way in code.
 
I know its up to me to do it, and as I said, I did do it, I was just looking for a bit of direction as to where/what I might be looking for. And I was also assuming (and I dont know why) that you may have used this code yourself before.

But, regardless, its done now.

My new code creates a grid on the page which fills the width of the page within the margins, and creates the columns in their orriginal width ratios.

Thanks again for your help
 
Why dont you use VBA with Excel, transfer the datagrid to excel and call the Excels print function. You have much more options and less code to write.

Make sure you hide the Excel app and also Dispose the objects correctly via the System.Runtime.InteropServices.Marshal.ReleaseComObject method.
 
Why dont you use VBA with Excel, transfer the datagrid to excel and call the Excels print function. You have much more options and less code to write.

Make sure you hide the Excel app and also Dispose the objects correctly via the System.Runtime.InteropServices.Marshal.ReleaseComObject method.
Works great, assuming everyone has Excel installed. That's a pretty big assumption in a lot of cases.
 
Back
Top