C # method class for printing multiple dview

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. drawing;
Using system. Drawing. printing;
Using system. Windows. forms;

Namespace demo
{
/// <Summary>
/// <Para> Print the datagridview </para>
/// <Para> usage: transmits the object of a datagridview array </para>
/// <Para> datagridviewprint dgvprint = new datagridviewprint (New dview [] {datagridview1, datagridview2}); </para>
/// <Para> dgvprint. Print (); </para>
/// </Summary>
Public class datagridviewprint
{
Private dview [] datagridview;
Private printdocument;
Private pagesetupdialog;
Private printpreviewdialog;

Private int dgvindex = 0;

Private int rowcount = 0;
Private int colcount = 0;
Private int x = 0;
Private int y = 0;
Int I = 0;

Private int rowgap = 60;
Private int leftmargin = 50;
Private Font font = new font ("Arial", 10 );
Private font headingfont = new font ("Arial", 11, fontstyle. Underline );
Private font captionfont = new font ("Arial", 10, fontstyle. Bold );
Private brush = new solidbrush (color. Black );
Private string cellvalue = string. empty;

Public datagridviewprint (datagridview [] datagridview)
{
This. datagridview = datagridview;
Printdocument = new printdocument ();
Printdocument. printpage + = new printpageeventhandler (this. printdocument_printpage );
}

Private void printdocument_printpage (Object sender, system. Drawing. Printing. printpageeventargs E)
{
For (; dgvindex <datagridview. length; dgvindex ++)
{

Rowcount = datagridview [dgvindex]. Rows. Count-1;

Colcount = datagridview [dgvindex]. columncount;

// Print headings
Y + = rowgap;
X = leftmargin;
For (Int J = 0; j <colcount; j ++)
{
If (datagridview [dgvindex]. Columns [J]. width> 0)
{
Cellvalue = datagridview [dgvindex]. Columns [J]. headertext;
E. Graphics. fillrectangle (New solidbrush (color. lightgray), X, Y, datagridview [dgvindex]. Columns [J]. Width, rowgap );
E. Graphics. drawrectangle (pens. Black, X, Y, datagridview [dgvindex]. Columns [J]. Width, rowgap );
E. Graphics. drawstring (cellvalue, headingfont, brush, x, y );
X + = datagridview [dgvindex]. Columns [J]. width;
}
}
// Print all rows
For (; I <rowcount; I ++)
{
Y + = rowgap;
X = leftmargin;
For (Int J = 0; j <colcount; j ++)
{
If (datagridview [dgvindex]. Columns [J]. width> 0)
{
Cellvalue = datagridview [dgvindex]. Rows [I]. cells [J]. value. tostring ();
E. Graphics. drawrectangle (pens. Black, X, Y, datagridview [dgvindex]. Columns [J]. Width, rowgap );
E. Graphics. drawstring (cellvalue, Font, brush, x, y );
X + = datagridview [dgvindex]. Columns [J]. width;
}
}
If (Y> = E. pagebounds. Height-80)
{
// Allow many snapshots to be printed
Y = 0;
E. hasmorepages = true;
I ++;

Return;
}
}
Y + = rowgap;
For (Int J = 0; j <colcount; j ++)
{
E. Graphics. drawstring ("", Font, brush, x, y );
}
I = 0;

}
E. hasmorepages = false;
}

Public printdocument getprintdocument ()
{
Return printdocument;
}

Public void print ()
{
Try
{
Pagesetupdialog = new pagesetupdialog ();
Pagesetupdialog. Document = printdocument;
Pagesetupdialog. showdialog ();
Printpreviewdialog = new printpreviewdialog ();
Printpreviewdialog. Document = printdocument;
Printpreviewdialog. Height = 600;
Printpreviewdialog. width = 800;
Printpreviewdialog. showdialog ();
}
Catch (exception E)
{
Throw new exception ("Printer error." + E. Message );
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.