Use. NET with the class implementation of the DataGrid report printing

Source: Internet
Author: User
Tags bool count rowcount
datagrid| print using System;
Using System.Windows.Forms;
Using System.Drawing;
Using System.Drawing.Printing;
Using System.Data;
Using System.Collections;
Using Datalibrary;

Namespace Controllibrary
{
<summary>
DataGrid printing
</summary>
public class Datagridprinter
{
Private PrintDocument Theprintdocument;
Private DataTable thetable;
Private DataGrid Thedatagrid;
public int rowcount = 0;
Private Const int kverticalcellleeway = 10;
public int pagenumber = 1;
Public ArrayList Lines = new ArrayList ();
public int header=0;
public int footer=0;
Datacatena DataCatena1;
public int pagewidth;
public int pageheight;
public int TopMargin;
public int bottommargin;
int gridwidth = 0;
public int LeftMargin;
public int RightMargin;
public string Papername;
public int PageCount;
private int rows_page;
public bool Showfooter;

public int Bodylength
{
Get
{
if (this. Pagenumber<this.pagecount)
Return (1+this.rows_page) * (int) (TheDataGrid.Font.SizeInPoints) + Kverticalcellleeway);
Else
Return (this. TheTable.Rows.Count + 1-(this.rows_page*) (this. PageNumber-1)) ((int) (TheDataGrid.Font.SizeInPoints) + Kverticalcellleeway);
}
}

Public Datagridprinter (DataGrid Agrid, PrintDocument aprintdocument,string theform,int Header,int)
{
//
Todo:add constructor Logic here
//
This.header=header;
This.footer=footer;
This.showfooter=true;
Thedatagrid = Agrid;
Theprintdocument = aprintdocument;
if (theform!= "")
{
Datacatena1=new Datacatena ();
DataView Mydv=datacatena1.getdataview ("select * from Yh_bbdy where bm= '" +theform + "");

This. papername=mydv[0]["ZM"]. ToString ();

PaperSize pksize;

if (mydv[0]["FX"]. ToString () = = "True")
This. Theprintdocument.defaultpagesettings.landscape=true;
Else
This. Theprintdocument.defaultpagesettings.landscape=false;

int found=0;
for (int i = 0; I < this. ThePrintDocument.PrinterSettings.PaperSizes.Count; i++)
{
Pksize = this. Theprintdocument.printersettings.papersizes[i];
if (pksize.papername==this. Papername)
{
This. Theprintdocument.defaultpagesettings.papersize=pksize;
found=1;
I=this. ThePrintDocument.PrinterSettings.PaperSizes.Count;
if (this. ThePrintDocument.DefaultPageSettings.Landscape)
{
PageHeight = Pksize.width;
PageWidth = Pksize.height;
}
Else
{
PageWidth = Pksize.width;
PageHeight = Pksize.height;
}
}
}

if (found==0)
{
if (this. ThePrintDocument.DefaultPageSettings.Landscape)
{
PageHeight = Int32.Parse (mydv[0]["ZK"). ToString ());
PageWidth = Int32.Parse (mydv[0]["Zc"). ToString ());
}
Else
{
PageWidth = Int32.Parse (mydv[0]["ZK"). ToString ());
PageHeight = Int32.Parse (mydv[0]["Zc"). ToString ());
}
}

TopMargin = Int32.Parse (mydv[0]["SK"). ToString ());
BottomMargin = Int32.Parse (mydv[0]["XK"). ToString ());
RightMargin = Int32.Parse (mydv[0]["RIGHTK"). ToString ());
LeftMargin = Int32.Parse (mydv[0]["LEFTK"). ToString ());
}
Else
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;
RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right;
LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left;
}

PageWidth = this. LeftMargin;
PageWidth = this. RightMargin;

this.rows_page= (int) (float) (this. Pageheight-this.topmargin-this.bottommargin-this.footer-this.header)/(TheDataGrid.Font.SizeInPoints + Kverticalcellleeway));

for (int k = 0; k < thedatagrid.tablestyles[0]. Gridcolumnstyles.count; k++)
{
Gridwidth + = Thedatagrid.tablestyles[0]. GRIDCOLUMNSTYLES[K]. Width; THETABLE.COLUMNS[K]. ToString ();
}

}

public void Drawheader (Graphics g)
{
SolidBrush Forebrush = new SolidBrush (Thedatagrid.headerforecolor);
SolidBrush Backbrush = new SolidBrush (color.white); (Thedatagrid.headerbackcolor);
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);
StringFormat CellFormat = new StringFormat ();
CellFormat. trimming = Stringtrimming.ellipsischaracter;
CellFormat. FormatFlags = Stringformatflags.nowrap | Stringformatflags.linelimit;
CellFormat. Alignment= Stringalignment.center;


int columnwidth = 0;

int initialrowcount = ROWCOUNT;

Draw the table header
float startxposition = this. LeftMargin; thedatagrid.location.x;
RectangleF nextcellbounds = new RectangleF (0,0, 0, 0);

/*
This section is the code that draws the background of the head.

RectangleF headerbounds = new RectangleF (0, 0, 0, 0);

Headerbounds.x = this. LeftMargin; thedatagrid.location.x;
Headerbounds.y = + (rowcount-initialrowcount) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway);
Headerbounds.height = TheDataGrid.Font.SizeInPoints + Kverticalcellleeway;
Headerbounds.width = PageWidth;
*/
float y1= (float) (header+this. TopMargin);

G.drawline (Thelinepen, this. LeftMargin, Y1, this. LeftMargin + this. PageWidth, y1);
y1=y1+ (float) (TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);
G.drawline (Thelinepen, this. LeftMargin, Y1, this. LeftMargin + this. PageWidth, y1);

G.fillrectangle (Backbrush, headerbounds);

for (int k = 0; k < this. Thedatagrid.tablestyles[0]. Gridcolumnstyles.count; k++)
{
ColumnWidth = Thedatagrid.tablestyles[0]. GRIDCOLUMNSTYLES[K]. Width * Pagewidth/this. Gridwidth;

String nextcolumn = Thedatagrid.tablestyles[0]. GRIDCOLUMNSTYLES[K]. HeaderText; THETABLE.COLUMNS[K]. ToString (); + (Rowcount-initialrowcount) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway)
RectangleF cellbounds = new RectangleF (startxposition, header + topmargin+2,
ColumnWidth
TheDataGrid.HeaderFont.SizeInPoints + kVerticalCellLeeway-2);
Nextcellbounds = Cellbounds;

if (startxposition + columnwidth <= pagewidth+this. LeftMargin)
{
g.DrawString (Nextcolumn, Thedatagrid.headerfont, Forebrush, Cellbounds, CellFormat);
}

Startxposition = startxposition + columnwidth;

}
y1=y1+ (float) (TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);
G.drawline (Thelinepen, this. LeftMargin, Y1, this. LeftMargin + this. PageWidth, y1);

if (Thedatagrid.gridlinestyle!= datagridlinestyle.none)
G.drawline (Thelinepen, thedatagrid.location.x, Nextcellbounds. Bottom, PageWidth, Nextcellbounds. Bottom);
}

public void Setdatasource (DataTable aTable)
{
thetable = aTable;
this.pagecount= (int) math.ceiling (double) (TheTable.Rows.Count)/(double) (this.rows_page);
MessageBox.Show ("Rows_count:" +thetable.rows.count.tostring () + "Rows_page:" +rows_page. ToString () + "Page_count:" +this.pagecount.tostring ());
This. Theprintdocument
}

public bool Drawrows (Graphics g)
{
int lastrowbottom = TopMargin;
Lines.clear ();

Try
{
SolidBrush Forebrush = new SolidBrush (Thedatagrid.forecolor);
SolidBrush Backbrush = new SolidBrush (Thedatagrid.backcolor);
SolidBrush Alternatingbackbrush = new SolidBrush (Thedatagrid.alternatingbackcolor);
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);
StringFormat CellFormat = new StringFormat ();
CellFormat. trimming = Stringtrimming.ellipsischaracter;
CellFormat. FormatFlags = Stringformatflags.nowrap | Stringformatflags.linelimit;
int columnwidth = Pagewidth/thedatagrid.tablestyles[0]. Gridcolumnstyles.count;

int initialrowcount = (this. PAGENUMBER-1) *this.rows_page;
int endrow=initialrowcount+this.rows_page;

RectangleF rowbounds = new RectangleF (0, 0, 0, 0);

Draw vertical Lines

Draw the table
for (int i = Initialrowcount i < TheTable.Rows.Count && i< Endrow; i++)
{
DataRow dr = Thetable.rows[i];
int startxposition = this. leftmargin+2; thedatagrid.location.x;

rowbounds.x = 0;
Rowbounds.y = header + TopMargin + ((rowcount-initialrowcount) +1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway );
Rowbounds.height = TheDataGrid.Font.SizeInPoints + Kverticalcellleeway;
Rowbounds.width = PageWidth;


/*///This section is the code to draw a table background
if (i%2 = 0)
{
G.fillrectangle (Backbrush, rowbounds);
}
Else
{
G.fillrectangle (Alternatingbackbrush, rowbounds);
}
*/

for (int j = 0; J < Thedatagrid.tablestyles[0]. Gridcolumnstyles.count; J + +)
{
ColumnWidth = Thedatagrid.tablestyles[0]. GRIDCOLUMNSTYLES[J]. width* Pagewidth/this. GridWidth-2;
RectangleF cellbounds = new RectangleF (startxposition,
Header + TopMargin + 2 +
((Rowcount-initialrowcount) + 1) * (TheDataGrid.Font.SizeInPoints + kverticalcellleeway),
ColumnWidth
TheDataGrid.Font.SizeInPoints + Kverticalcellleeway);


if (startxposition + columnwidth <= pagewidth)
{
g.DrawString (dr[(thedatagrid.tablestyles[0). GRIDCOLUMNSTYLES[J]. MappingName)]. ToString (), Thedatagrid.font, Forebrush, Cellbounds, CellFormat);
}

Startxposition = startxposition + columnwidth+2;
}

Lines.add (Rowbounds.bottom);
Lastrowbottom = (int) rowbounds.bottom;

rowcount++;
}

Drawhorizontallines (g, Lines);
Drawverticalgridlines (g, thelinepen,lastrowbottom);

if (this.showfooter==true)
Drawfooter (g);

if (this. Pagenumber<this.pagecount)
{
return true;
}
Else
{
return false;
}

}
catch (Exception ex)
{
MessageBox.Show (ex. Message.tostring ());

return false;
}

}

void Drawfooter (Graphics g)
{
FontFamily FontFamily = new FontFamily ("Song Body");

String text= "+this". Pagenumber.tostring () + "page Total"
+this.pagecount.tostring () + "page";

Font font = new Font (
FontFamily, 10,
Fontstyle.regular,
Graphicsunit.point);

SizeF size= g.measurestring (Text,font);

int startpos = this. PageHeight
-this. BottomMargin;

SolidBrush Forebrush = new SolidBrush (color.black);

StringFormat CellFormat = new StringFormat ();

g.DrawString (Text,font,forebrush,
(This. Pagewidth-size.width)/2 + this. LeftMargin,
Startpos, CellFormat);

text= "" Cobainsoft "";

Size= g.measurestring (Text,font);

g.DrawString (Text,font,forebrush,
This. PageWidth + this. Leftmargin-size. Width-3,
Startpos, CellFormat);

}



void Drawhorizontallines (Graphics g, ArrayList Lines)
{
Pen Thelinepen = new Pen (thedatagrid.gridlinecolor, 1);

if (Thedatagrid.gridlinestyle = = Datagridlinestyle.none)
Return

int i;

for (i = 0; i < lines. Count; i++)
{
G.drawline (Thelinepen, this. LeftMargin, (float) lines[i], this. LeftMargin + this. PageWidth, (float) lines[i]);
}

}

void Drawverticalgridlines (Graphics g, Pen thelinepen, int bottom)
{
if (Thedatagrid.gridlinestyle = = Datagridlinestyle.none)
Return

int posx=0;

Draw the left-most vertical line

G.drawline (Thelinepen, this. LeftMargin + POSX,
Header + TopMargin,
This. LeftMargin + POSX,
bottom);

Draw the vertical bar to the right
G.drawline (Thelinepen, this. LeftMargin + this. PageWidth,
Header + TopMargin,
This. LeftMargin + this. PageWidth,
bottom);

int total=thedatagrid.tablestyles[0]. gridcolumnstyles.count-1;
for (int k = 0; k < total; k++)
{
posx+= (Thedatagrid.tablestyles[0]. GRIDCOLUMNSTYLES[K]. Width) * this. Pagewidth/this. Gridwidth;
G.drawline (Thelinepen, this. LeftMargin + POSX,
Header + TopMargin,
This. LeftMargin + POSX,
bottom);
}
}


public bool Drawdatagrid (Graphics g)
{

Try
{
Drawheader (g);
BOOL bcontinue = Drawrows (g);
return bcontinue;
}
catch (Exception ex)
{
MessageBox.Show (ex. Message.tostring ());
return false;
}

}

}

}




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.