欢迎光临
我们一直在努力

使用.NET自带的类实现DataGrid报表的打印-.NET教程,报表/图形/Office

建站超值云服务器,限时71元/月

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打印

/// </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 footer)

{

//

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

/*

这一段是画表头底色的代码

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 rows of 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;

/* ///这一段是画表格底色的代码

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("宋体");

string text="第"+this.pagenumber.tostring()+"页 共"

+this.pagecount.tostring()+"页";

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;

//画最左边的竖线

g.drawline(thelinepen, this.leftmargin + posx,

header + topmargin,

this.leftmargin + posx,

bottom);

//画最右边的竖线

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;

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用.NET自带的类实现DataGrid报表的打印-.NET教程,报表/图形/Office
分享到: 更多 (0)

相关推荐

  • 暂无文章