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

datagrid|打印

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

}

}

}

时间: 2025-01-01 10:57:16

使用.NET自带的类实现DataGrid报表的打印的相关文章

ios-自定义类系统自带的类模板没有了、怎么办啊!

问题描述 自定义类系统自带的类模板没有了.怎么办啊! 每次新建继承系统的类的文件都是光秃秃的!太不方便了,原来新建都会自带类模板的啊?求解 解决方案 类模板多种类型的类模板自定义类模板,类模板的默认类型数组的模板实现,友元和类模板,友元函数,类模板与静态变量,类模板与普通类之间互相继承,类模板作为模板参数,类嵌套,类模板嵌套,类包装器 解决方案二: 楼主指的是什么环境什么编辑器 解决方案三: 我觉得你是不是直接选了Objective-C文件,而不是cocoa touch class

Visio与vs2005自带的“类关系图”功能哪个更好?好在哪里?

问题描述 Visio与vs2005自带的"类关系图"功能哪个更好?好在哪里? 解决方案 解决方案二:在线等解决方案三:visio的界面看起来没vs2005的"类关系图"界面舒服,这个软件好像比较老了,vs2008里不是也加强了"类关系图"功能吗?这样比起来呢?还是visio更专业吗?解决方案四:visio比较好用一点解决方案五:Visio肯定更专业点,初学者用用类关系图蛮方便的

【IOS-COCOS2D-X 游戏开发之五】游戏存储之COCOS2DX自带CCUSERDEFAULT类详解;

本站文章均为 李华明Himi 原创,转载务必在明显处注明:  转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/653.html 本篇跟大家分享下Cocos2dX中的存储,其中也介绍些细节容易犯错的问题: 在Cocos2dX中提供了自带存储类:CCUserDefault  ,当然了这里Himi强调一点,如果你的数据量比较大,建议使用SQL存储比较适合,另外一点要注意的是,尽可能不要在Cocos2dX中使用与平台相关的ap

一个通用的Datagrid导出Excel打印的源函数

datagrid|excel|打印|导出excel|函数 一个通用的Datagrid导出Excel打印的源函数闲暇之余,写成函数,供新人研究学习 'Power by:Landlordh '列宽默认为datagird的tablestyles(0)列宽的五分之一 'G2E(dg1) Public Function G2E(ByVal dg As DataGrid) Dim dt As New DataTable Try dt = CType(dg.DataSource, DataTable) Cat

codeigniter自带数据库类使用方法说明

 在 CodeIgniter 中,使用数据库是非常频繁的事情.你可以使用框架自带的数据库类,就能便捷地进行数据库操作 初始化数据库类   依据你的数据库配置载入并初始化数据库类:   代码如下: this->load->database();     被载入之后你可以在任何地方使用它.   以对象形式返回查询结果    代码如下: $query = $this->db->query('SELECT name, title, email FROM my_table');   fore

codeigniter自带数据库类使用方法说明_php实例

初始化数据库类 依据你的数据库配置载入并初始化数据库类: 复制代码 代码如下: this->load->database(); 被载入之后你可以在任何地方使用它. 以对象形式返回查询结果 复制代码 代码如下: $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() as $row){    echo $row->title;    ec

调用带参类的带参方法- 如何把字符串转换成用字符串显示的字符类型 求帮助!

问题描述 如何把字符串转换成用字符串显示的字符类型 求帮助! private String[] colName = null; // 列名数组private String[] colType = null; //存放数据类型private String[] colValue = null; // 列植 这里有三个数组是从数据库中读取这些数据 然后将这些数据转换成一个对象 老师留的仿hibernate的根据主键获取对象的方法 我知道要用类反射做 可是怎么用就不太明白了 我写了这么个代码 Class

实用的PHP带公钥加密类分享(每次加密结果都不一样哦)_php实例

WEB交互安全一直是个各大网站的首要解决方案,本文介绍的PHP加密类非常实用哦,带有公钥,这是最大的亮点,没有公钥是不能解密的,加密度非常高. 类代码: <?php /** * PHP加密类 * 琼台博客 */ class Jiami{ // 公钥 protected $key = 'lee'; private function keyED($txt,$encrypt_key){ $encrypt_key = md5($encrypt_key); $ctr=0; $tmp = ''; for (

用ASP.Net中的自带类进行MD5加密

其实在ASP.net编程中,不用调用md5.asp来加密数据.在DotNet中有自带的类:System.Web.Security.HashPasswordForStoringInConfigFile() 以下是引用片段: public string md5(string str,int code) { if(code==16) //16位MD5加密(取32位加密的9~25字符) { return System.Web.Security.FormsAuthentication.HashPasswo