Printing all data in datagrid?

  • Thread starter Thread starter Veronika
  • Start date Start date
V

Veronika

Hi all,
I need to print all the data in my datagrid to a printer, and the user
must be able to choose whether with the grid lines or without. I am a
beginner in .net and I would appreciate any help!!!
Thanks,
Veronika
 
hi
you will be using the drawing namespace when you are writing to the
printer .... so actually you will be drawing the lines and even the values
form the datagrid one by one.(this is the way to write to the printer,
using drawing)
what you can do it to ues a check box that set a boolean
variable"use would check it if he wants to draw lines". then check this
value inside the printpage handler, if it is set, then use the drawline
method of drawing between values , otherwise, you skip this step .....
hope that would help
 
Yea, thanks, that helps a lot. Do I have to go through the datagrid or
the dataset? And I looked at code samples for printing, and it doesn't
make any sense. They use a class derived from the printDocument class,
and X and Y positions. Is that the easiest way to go about it? It looks
quite complicated. Does anyone have a piece of code for the printing? It
looks like all the examples I have been looking at don't only do the
printing...

Thanks a lot!!!
 
Hi
Look in the simplest form . you will be adding a PrintDocument object to
your class
private System.Drawing.Printing.PrintDocument printDocument1;
you might add a printdialog and set its printdocument properety to your
print document.

You might also need to add PageSettings and assign it to your printdocument.

PageSettings myPageSettings=new PageSettings();
printDocument1.DefaultPageSettings=myPageSettings;

the only thing that you have to do in order to print is to write a handler
to the printpage event of the printdocumnet object


private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{

e.Graphics.PageUnit=GraphicsUnit.Millimeter; //set the mode that you
will be using

//then to write values form the dataset or the datagrid this is your
decision
To write just use
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{


e.Graphics.DrawString("these are the values that are inside my dataset");


e.Graphics.DrawString(ds.Tables[0][0].Tostring(););
//after you enter one complete row you can then draw a line before
//starting the new row
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)),0,0,100,100);

//then you check to see if you are done or not, if not set the has //more
pages to true so the print event would be fired again to print //more pages
If(//your test condition , the data in the dataset is done or not may be){
e.HasMorePages =true;
}else { e.HasMorePages =false;
}

}

hope that would help
 
Back
Top