Multifunction DataGrid Printing Class (WinForm C #)

Source: Internet
Author: User
Tags bool count header include rowcount tostring
datagrid| Printing can achieve the printing function as shown above.

• All fonts, margins, header height, row height, can be customized.

• Supports automatic calculation of the number of rows per page and fixed rows per page.

• Support footer display pages.



Because of their own use and I am lazy, so the attributes are set to public, the time to assign value carefully.

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Drawing.Printing;
Using System.Data;

Using System.Windows.Forms;

Namespace Cjmanager
{
public class Cuteprinter
{
private DataGrid DataGrid;
Private PrintDocument PrintDocument;
Private PageSetupDialog PageSetupDialog;
Private PrintPreviewDialog PrintPreviewDialog;

private string Title= "";

int currentpageindex=0;
int rowcount=0;
int pagecount=0;

int titlesize=16;
BOOL Iscustomheader=false;

Brush alertbrush=new SolidBrush (color.red);

String[] header=null;//If a custom is filled in with a string, if a slash is required, use \ notation, for example: number # name where # is Splitchar
String[] uplineheader=null;//Ascending text array
Int[] uplineheaderindex=null;//up the text index, if not uplink set to 1;
BOOL iseverypageprinthead=true;//whether to print column headers on every page.


public bool iseverypageprinttitle=false;//Whether a title is to be printed on every page.
public int headerheight=50;//Header height.
public int topmargin=60; Top margin
public int celltopmargin=6;//cell top margin
public int cellleftmargin=4;//Cell left margin
public Char splitchar= ' # ';//When the header is to be represented by a slash
The public string falsestr= "X";//If False in the DataGrid passed in, converts it to a character.
The public string truestr= "√";//If True in the DataGrid being passed in, converts it to a character.
public int pagerowcount=7;//The number of rows per page
public int rowgap = 30;//Row Height
public int colgap = 5;//per column interval
public int leftMargin = 50;//Left margin
Public font titlefont=new font ("Arial", 14);//Heading Fonts
Public font font = new Font ("Arial", 10);//Body font
Public Font HeaderFont = new Font ("Arial", 9, FontStyle.Bold);//Column name title
Public font footerfont=new fonts ("Arial", 8);//Footer Displays the number of pages
Public font uplinefont=new font ("Arial", 9, FontStyle.Bold)////When the header is displayed in two rows, the fonts displayed on the uplink.
Public font underlinefont=new font ("Arial", 8)///When the header is displayed in two rows, the line appears.
Public Brush Brush = new SolidBrush (color.black);//Painting brush
public bool isautopagerowcount=true;//Whether the line count is automatically calculated.
public int buttommargin=80;//Bottom margin
public bool needprintpageindex=true;//whether to print footer pages

String Filterstr= "";





Public Cuteprinter (DataGrid datagrid,string title,int titlesize)
{
This.title=title;
This.titlesize=titlesize;


This.datagrid = DataGrid;
PrintDocument = new PrintDocument ();
Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);


}
Public Cuteprinter (DataGrid datagrid,string title)
{
This.title=title;


This.datagrid = DataGrid;
PrintDocument = new PrintDocument ();
Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);
}
Public Cuteprinter (DataGrid DataGrid)
{
This.datagrid = DataGrid;
PrintDocument = new PrintDocument ();
Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);
}

public bool Settowlineheader (string[] uplineheader,int[] uplineheaderindex)
{
This.uplineheader=uplineheader;
This.uplineheaderindex=uplineheaderindex;
This.iscustomheader=true;
return true;
}
public bool SetHeader (string[] header)
{
This.header=header;
return true;

}

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

int width=e.pagebounds.width;
int height=e.pagebounds.height;

if (This.isautopagerowcount)
pagerowcount= (int) (Height-this.topmargin-titlesize-this.headerfont.height-this.headerheight-this.buttommargin )/this.rowgap);

pagecount= (int) (rowcount/pagerowcount);
if (rowcount%pagerowcount>0)
pagecount++;

int xoffset= (int) (WIDTH-E. Graphics.measurestring (This.title,this.titlefont). Width)/2);
int colcount = 0;
int x = 0;
int y =topmargin;
String cellvalue = "";

int startrow=currentpageindex*pagerowcount;
int endrow=startrow+this.pagerowcount<rowcount?startrow+pagerowcount:rowcount;
int currentpagerowcount=endrow-startrow;


if (this.currentpageindex==0 | | this.iseverypageprinttitle)
{
E.graphics.drawstring (This.title,titlefont,brush,xoffset,y);
Y+=titlesize;
}



ColCount = Datagrid.tablestyles[0]. Gridcolumnstyles.count;

Y + + rowgap;
x = LeftMargin;


DrawLine (new Point (X,y), new Point (X,y+currentpagerowcount*rowgap+this.headerheight), e.graphics);//Leftmost vertical bar

int lastindex=-1;
int lastlength=0;
int indexj=-1;

for (int j = 0; J < ColCount; J + +)
{
int colwidth=datagrid.tablestyles[0]. GRIDCOLUMNSTYLES[J]. Width;
if (colwidth> 0)
{
indexj++;
if (This.header==null | | this.header[indexj]== "")
Cellvalue = Datagrid.tablestyles[0]. GRIDCOLUMNSTYLES[J]. HeaderText;
Else
CELLVALUE=HEADER[INDEXJ];

if (This.iscustomheader)
{
if (This.uplineheaderindex[indexj]!=lastindex)
{

if (lastlength>0 && lastindex>-1)//Start Drawing previous Upline
{
String Uplinestr=this.uplineheader[lastindex];
int upxoffset= (int) (Lastlength-e.graphics.measurestring (Uplinestr,this.uplinefont). Width)/2);
if (upxoffset<0)
upxoffset=0;
E.graphics.drawstring (uplinestr,this.uplinefont,brush,x-lastlength+upxoffset,y+ (int) (THIS.CELLTOPMARGIN/2));

DrawLine (x-lastlength,y+ (int) (THIS.HEADERHEIGHT/2)), New Point (x,y+ (int) (THIS.HEADERHEIGHT/2)), E. Graphics);/midline
DrawLine (new Point (X,y), New Point (x,y+ (int) (THIS.HEADERHEIGHT/2)), e.graphics);
}
LASTINDEX=THIS.UPLINEHEADERINDEX[INDEXJ];
Lastlength=colwidth+colgap;
}
Else
{
Lastlength+=colwidth+colgap;
}
}

int currenty=y+celltopmargin;


int xoffset=10;
int yoffset=20;
int Leftwordindex=cellvalue.indexof (THIS.SPLITCHAR);
if (this.uplineheaderindex!=null && this.uplineheaderindex[indexj]>-1)
{

if (leftwordindex>0)
{
String leftword=cellvalue.substring (0,leftwordindex);
String rightword=cellvalue.substring (leftwordindex+1,cellvalue.length-leftwordindex-1);
The words above
xoffset= (int) (Colwidth+colgap-e.graphics.measurestring (Rightword,this.uplinefont). Width);
yoffset= (int) (This.headerheight/2-e.graphics.measurestring ("a", This.underlinefont). Height);


xoffset=6;
yoffset=10;
E.graphics.drawstring (rightword,this.underlinefont,brush,x+xoffset-4,y+ (int) (THIS.HEADERHEIGHT/2));
E.graphics.drawstring (leftword,this.underlinefont,brush,x+2,y+ (int) (THIS.HEADERHEIGHT/2) + (int) ( THIS.CELLTOPMARGIN/2) +yoffset-2);
DrawLine (x,y+ (int) (THIS.HEADERHEIGHT/2), new Point (X+colwidth+colgap,y+headerheight), e.graphics);
X + + colwidth + colgap;
DrawLine (x,y+ (int) (THIS.HEADERHEIGHT/2), new Point (X,y+currentpagerowcount*rowgap+this.headerheight), E . Graphics);
}
Else
{

E.graphics.drawstring (Cellvalue, HeaderFont, Brush, X, y+ (int) (THIS.HEADERHEIGHT/2) + (int) (THIS.CELLTOPMARGIN/2));
X + + colwidth + colgap;
DrawLine (x,y+ (int) (THIS.HEADERHEIGHT/2), new Point (X,y+currentpagerowcount*rowgap+this.headerheight), E . Graphics);
}

}
Else
{
if (leftwordindex>0)
{
String leftword=cellvalue.substring (0,leftwordindex);
String rightword=cellvalue.substring (leftwordindex+1,cellvalue.length-leftwordindex-1);
The words above
xoffset= (int) (Colwidth+colgap-e.graphics.measurestring (Rightword,this.uplinefont). Width);
yoffset= (int) (This.headerheight-e.graphics.measurestring ("a", This.underlinefont). Height);

E.graphics.drawstring (rightword,this.headerfont,brush,x+xoffset-4,y+2);
E.graphics.drawstring (leftword,this.headerfont,brush,x+2,y+yoffset-4);
DrawLine (new Point (X,y), new Point (X+colwidth+colgap,y+headerheight), e.graphics);
X + + colwidth + colgap;
DrawLine (new Point (X,y), new Point (X,y+currentpagerowcount*rowgap+this.headerheight), e.graphics);
}
Else
{
E.graphics.drawstring (Cellvalue, HeaderFont, Brush, X, Y+celltopmargin);
X + + colwidth + colgap;
DrawLine (new Point (X,y), new Point (X,y+currentpagerowcount*rowgap+this.headerheight), e.graphics);
}

}

}
}
End of Loop, draw the last Upline
if (This.iscustomheader)
{

if (lastlength>0 && lastindex>-1)//Start Drawing previous Upline
{
String Uplinestr=this.uplineheader[lastindex];
int upxoffset= (int) (Lastlength-e.graphics.measurestring (Uplinestr,this.uplinefont). Width)/2);
if (upxoffset<0)
upxoffset=0;
E.graphics.drawstring (uplinestr,this.uplinefont,brush,x-lastlength+upxoffset,y+ (int) (THIS.CELLTOPMARGIN/2));

DrawLine (x-lastlength,y+ (int) (THIS.HEADERHEIGHT/2)), New Point (x,y+ (int) (THIS.HEADERHEIGHT/2)), E. Graphics);/midline
DrawLine (new Point (X,y), New Point (x,y+ (int) (THIS.HEADERHEIGHT/2)), e.graphics);
}

}

int rightbound=x;

DrawLine (new Point (Leftmargin,y), new Point (Rightbound,y), e.graphics); The top line.

DrawLine (new Point (Leftmargin,y+this.headerheight), new Point (Rightbound,y+this.headerheight), e.graphics); The line below the column name

Y+=this.headerheight;


Print all Rows
for (int i = StartRow i < Endrow; i++)
{

x = LeftMargin;
for (int j = 0; J < ColCount; J + +)
{
if (datagrid.tablestyles[0). GRIDCOLUMNSTYLES[J]. Width > 0)
{
Cellvalue = Datagrid[i,j]. ToString ();
if (cellvalue== "False")
CELLVALUE=FALSESTR;
if (cellvalue== "True")
CELLVALUE=TRUESTR;

E.graphics.drawstring (cellvalue, font, brush, x+this.cellleftmargin, y+celltopmargin);
x + + datagrid.tablestyles[0]. GRIDCOLUMNSTYLES[J]. Width + colgap;
y = y + rowgap * (Cellvalue.split (new char[] {' \ r ', ' \ n '}). LENGTH-1);
}
}
DrawLine (new Point (Leftmargin,y), new Point (Rightbound,y), e.graphics);
Y + + rowgap;
}
DrawLine (new Point (Leftmargin,y), new Point (Rightbound,y), e.graphics);

currentpageindex++;

if (This.needprintpageindex)
E.graphics.drawstring ("Total" +pagecount.tostring () + "page, current section" +this.currentpageindex.tostring () + "page", This.footerfont, brush,width-200, (int) (height-this.buttommargin/2-this.footerfont.height));

string s = cellvalue;
string f3 = Cellvalue;



if (Currentpageindex<pagecount)
{
E.hasmorepages=true;
}
Else
{
E.hasmorepages=false;
this.currentpageindex=0;

}
Ipagenumber+=1;


}
private void DrawLine (Point sp,point ep,graphics GP)
{
Pen pen=new Pen (color.black);
Gp. DrawLine (PEN,SP,EP);
}

Public PrintDocument Getprintdocument ()
{
return PrintDocument;
}



public void Print ()
{



rowcount=0;

if (DataGrid.DataSource.GetType (). ToString () = = "System.Data.DataTable")
{
RowCount = ((DataTable) datagrid.datasource). Rows.Count;
}
else if (DataGrid.DataSource.GetType (). ToString () = = "System.Collections.ArrayList")
{
RowCount = ((ArrayList) datagrid.datasource). Count;
}


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);
}

}
}
}

Use example to display the result as a top figure.

private void Bnprint_click (object sender, System.EventArgs e)
{

Cuteprinter dgp=new Cuteprinter (this.datagrid1,this.dlsearchyear.text+ "year" + "professional", 16);
String[] uplinestr={"hehe, hehe", "Xixi"};
String[] header={"hehe #xixi", "HEE#XCC", "KDFJ#DJJJ", "Kk#jj", "KDJF", "", "", "", ""};
Dgp.setheader (header);//If you don't need to change the header, you don't have to assign a value.
Note that the column here does not include the Width==0 column
Int[] uplineindex={-1,-1,0,0,0,-1,1,1};//Note that the column here does not include the Width==0 column
Dgp.settowlineheader (Uplinestr,uplineindex);
Dgp. Print ();
}




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.