首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 认证考试-> 软件水平考试
多功能DataGrid打印类(WinForm C#)-.NET教程,C#语言
作者:网友供稿 点击:385
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
能实现如上图的的打印功能。

·所有字体,边距,header 高,行高,都可以自定义。

·支持自动计算每页行数与每页固定行数。

·支持页脚显示页数。



由于自己用和本人比较懒,所以把属性都设置成公有,赋值的时候小心。

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;//如果自定义就填入字符串,如果需要斜线分隔,就用\表示,例如:个数#名字 其中#为splitchar
string[] uplineheader=null;//上行文字数组
int[] uplineheaderindex=null;//上行的文字index,如果没有上行就设为-1;
//bool iseverypageprinthead=true;//是否每一页都要打印列头。


public bool iseverypageprinttitle=false;//是否每一页都要打印标题。
public int headerheight=50;//标题高度。
public int topmargin=60; //顶边距
public int celltopmargin=6;//单元格顶边距
public int cellleftmargin=4;//单元格左边距
public char splitchar=#;//当header要用斜线表示的时候
public string falsestr="×";//如果传进来的datagrid中有 false,把其转换得字符。
public string truestr="√";//如果传进来的datagrid中有 true,把其转换得字符。
public int pagerowcount=7;//每页行数
public int rowgap = 30;//行高
public int colgap = 5;//每列间隔
public int leftmargin = 50;//左边距
public font titlefont=new font("arial",14);//标题字体
public font font = new font("arial", 10);//正文字体
public font headerfont = new font("arial", 9, fontstyle.bold);//列名标题
public font footerfont=new font("arial",8);//页脚显示页数的字体
public font uplinefont=new font("arial",9, fontstyle.bold);//当header分两行显示的时候,上行显示的字体。
public font underlinefont=new font("arial",8);//当header分两行显示的时候,下行显示的字体。
public brush brush = new solidbrush(color.black);//画刷
public bool isautopagerowcount=true;//是否自动计算行数。
public int buttommargin=80;//底边距
public bool needprintpageindex=true;//是否打印页脚页数

//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);//最左边的竖线

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)//开始画上一个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(new point(x-lastlength,y+(int)(this.headerheight/2)),new point(x,y+(int)(this.headerheight/2)),e.graphics);//中线
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);
//上面的字
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(new point(x,y+(int)(this.headerheight/2)),new point(x+colwidth+colgap,y+headerheight),e.graphics);
x += colwidth + colgap;
drawline(new point(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(new point(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);
//上面的字
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);
}

}

}
}
////循环结束,画最后一个的upline
if(this.iscustomheader)
{

if(lastlength>0 && lastindex>-1)//开始画上一个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(new point(x-lastlength,y+(int)(this.headerheight/2)),new point(x,y+(int)(this.headerheight/2)),e.graphics);//中线
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); //最上面的线

//drawline(new point(leftmargin,y+this.headerheight),new point(rightbound,y+this.headerheight),e.graphics);//列名的下面的线

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("共 "+pagecount.tostring()+" 页,当前第 "+this.currentpageindex.tostring()+" 页",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);
}

}
}
}

//用法示例,显示结果如顶图。

private void bnprint_click(object sender, system.eventargs e)
{

cuteprinter dgp=new cuteprinter(this.datagrid1,this.dlsearchyear.text+"年"+"专业",16);
string[] uplinestr={"呵呵,hehe","xixi"};
string[] header={"呵呵#xixi","hee#xcc","kdfj#djjj","kk#jj","kdjf","","","",""};
dgp.setheader(header);//如果不用改原header就不用赋值。
//注意,这里的列不包括width==0的列
int[] uplineindex={-1,-1,0,0,0,-1,1,1};//注意,这里的列不包括width==0的列
dgp.settowlineheader(uplinestr,uplineindex);
dgp.print();
}



文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·省市联动菜单的源代码实现(access数据库版)-ASP教程,数据库相关
·【HTML】打开页面时按照指定的URL直接跳转到另一新页面-ASP教程,脚本编码
·多功能DataGrid打印类(WinForm C#)-.NET教程,C#语言
·我也来谈WEB打印。-ASP教程,打印相关
·C#中的泛型 (From dotNet SDK 2.0 Beta1)-.NET教程,C#语言
·J2EE WEB层的曙光 -- JSF-JSP教程,J2EE/EJB/服务器
·C#:获得文件版本信息及只读文件的删除-.NET教程,C#语言
·[ASP.NET]按键跳转以及按Enter以不同参数提交,及其他感应事件-.NET教程,Asp.Net开发
·谈基于.net平台windows开发中的模式窗体-.NET教程,.NET Framework
·ASP.NET 2.0 中实现 Treeview 与数据库的绑定(含代码)-.NET教程,Asp.Net开发
最新文章
·中文版windows xp系统升级sp3新鲜体验_windows xp
·十个提高windows vista系统性能的秘密_windows vista
·用搏客blog月收入200美圆_网赚技巧
·google 禁止使用在线广告宣传adsense推介_网赚技巧
·做广告联盟的站长必读规则_网赚技巧
·刘润:用c2c模式运作捐献公益组织_站长访谈
·seo资料站长丘仕达访谈:整站优化心得_站长访谈
·易被忽视的优化网站设计的五种方法_站长心得
·photoshop非主流照片:可爱红脸蛋美女_photoshop教程
·如何让网民爱上你的网站?(1)_站长心得
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭