分享
 
 
 

使用.NET自带的类实现DataGrid报表的打印

王朝c#·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Printing;

using System.Data;

using System.Collections;

using DataLibrary;

namespace ControlLibrary

{

///

/// DataGrid打印

///

public class DataGridPrinter

{

private PrintDocument ThePrintDocument;

private DataTable TheTable;

private DataGridTheDataGrid;

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

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

{

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

{

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

{

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

{

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.Locatio

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有