| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 863 人关注过本帖
标题:关于datagrid的打印
只看楼主 加入收藏
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
收藏
 问题点数:0 回复次数:2 
关于datagrid的打印
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Collections;

namespace Sx_Mdi
{
/// <summary>
/// Summary description for DataGridPrinter.
/// </summary>
public class DataGridPrinter
{

private PrintDocument ThePrintDocument;
private DataTable TheTable;
private DataGrid TheDataGrid;

public int RowCount = 0; // current count of rows;
private const int kVerticalCellLeeway = 10;
public int PageNumber = 1;

int PageWidth;
int PageHeight;
int TopMargin;
int BottomMargin;

public string receieve_amount; //收货数量
public string receieve_moneys; //收货金额
public string send_amount; //发货数量
public string send_moneys; //发货金额


public DataGridPrinter(DataGrid aGrid, PrintDocument aPrintDocument, DataTable aTable)
{
//
// TOD Add constructor logic here
//
TheDataGrid = aGrid;
ThePrintDocument = aPrintDocument;
TheTable = aTable;


//得到打印参数
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom;

}

public void GetValues(string sh_amount,string sh_moneys,string fh_amount,string fh_moneys)
{
//
// TOD Add constructor logic here
//
receieve_amount = sh_amount;
receieve_moneys = sh_moneys;
send_amount = fh_amount;
send_moneys = fh_moneys;


}

public void DrawHeader(Graphics g)
{
SolidBrush ForeBrush = new SolidBrush(TheDataGrid.HeaderForeColor);
SolidBrush BackBrush = new SolidBrush(TheDataGrid.HeaderBackColor);
Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);
StringFormat cellformat = new StringFormat();
cellformat.Trimming = StringTrimming.EllipsisCharacter;
cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;


g.DrawString("收发对比表",new Font("Arial", 20, FontStyle.Bold), new SolidBrush(TheDataGrid.HeaderBackColor), PageWidth/2 , TopMargin, new StringFormat());

int columnwidth = PageWidth/TheTable.Columns.Count - 20;


int initialRowCount = RowCount;

// draw the table header
float startxposition = TheDataGrid.Location.X;
RectangleF nextcellbounds = new RectangleF(0,0, 0, 0);

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

HeaderBounds.X = TheDataGrid.Location.X;
HeaderBounds.Y = TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
HeaderBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
HeaderBounds.Width = PageWidth;

g.FillRectangle(BackBrush, HeaderBounds);

for (int k = 0; k < TheTable.Columns.Count; k++)
{

string nextcolumn = TheTable.Columns[k].ToString();
switch(nextcolumn)
{
case "code":
nextcolumn ="收货单号";
break;
case "Bp_Code":
nextcolumn = "衣服编号";
break;
case "Bp_Name":
nextcolumn = "衣服名称";
break;
case "receieve_prices":
nextcolumn = "收货价格";
break;
case "receieve_amounts":
nextcolumn = "收货数量";
break;
case "receieve_moneys":
nextcolumn = "收货金额";
break;
case "send_amunts":
nextcolumn = "发货数量";
break;
case "send_prices":
nextcolumn = "发货价格[元/打]";
break;
case "send_moneys":
nextcolumn = "发货金额";
break;
default:
break;
}

RectangleF cellbounds = new RectangleF(startxposition, TheDataGrid.Location.Y + TopMargin + (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.HeaderFont.SizeInPoints + kVerticalCellLeeway);
nextcellbounds = cellbounds;

if (startxposition + columnwidth <= PageWidth)
{

g.DrawString(nextcolumn, TheDataGrid.HeaderFont, ForeBrush, cellbounds, cellformat);
}

startxposition = startxposition + columnwidth;


}

if (TheDataGrid.GridLineStyle != DataGridLineStyle.None)
g.DrawLine(TheLinePen, TheDataGrid.Location.X, nextcellbounds.Bottom, PageWidth, nextcellbounds.Bottom);
}

public bool DrawRows(Graphics g)
{
int lastRowBottom = TopMargin;

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/TheTable.Columns.Count - 20;

int initialRowCount = RowCount;

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

// draw vertical lines

ArrayList Lines = new ArrayList();

// draw the rows of the table


for (int i = initialRowCount; i < TheTable.Rows.Count; i++)
{

DataRow dr = TheTable.Rows[i];
int startxposition = TheDataGrid.Location.X;

RowBounds.X = TheDataGrid.Location.X;
RowBounds.Y = TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount)+1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);
RowBounds.Height = TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway;
RowBounds.Width = PageWidth;
Lines.Add(RowBounds.Bottom);

if (i%2 == 0)
{
g.FillRectangle(BackBrush, RowBounds);
}
else
{
g.FillRectangle(AlternatingBackBrush, RowBounds);
}


for (int j = 0; j < TheTable.Columns.Count; j++)
{



RectangleF cellbounds = new RectangleF(startxposition,
TheDataGrid.Location.Y + TopMargin + ((RowCount - initialRowCount) + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);


if (startxposition + columnwidth <= PageWidth)
{
g.DrawString(dr[j].ToString(), TheDataGrid.Font, ForeBrush, cellbounds, cellformat);
lastRowBottom = (int)cellbounds.Bottom;
}

startxposition = startxposition + columnwidth;
}


RowCount++;

if (RowCount * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway) > (PageHeight * PageNumber) - (BottomMargin+TopMargin))
{
DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);
return true;
}


}

DrawHorizontalLines(g, Lines);
DrawVerticalGridLines(g, TheLinePen, columnwidth, lastRowBottom);


RectangleF cellbound1 = new RectangleF(TheDataGrid.Location.X,
TheDataGrid.Location.Y + TopMargin + (RowCount + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);

g.DrawString("合计:", TheDataGrid.Font, ForeBrush, cellbound1, new StringFormat());


RectangleF cellbound2 = new RectangleF(TheDataGrid.Location.X+columnwidth,
TheDataGrid.Location.Y + TopMargin + (RowCount + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);

g.DrawString(this.receieve_amount .ToString (), TheDataGrid.Font, ForeBrush, cellbound2, new StringFormat());


RectangleF cellbound3 = new RectangleF(TheDataGrid.Location.X+2*columnwidth,
TheDataGrid.Location.Y + TopMargin + (RowCount + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);

g.DrawString(this.receieve_moneys .ToString (), TheDataGrid.Font, ForeBrush, cellbound3, new StringFormat());

RectangleF cellbound4 = new RectangleF(TheDataGrid.Location.X+3*columnwidth,
TheDataGrid.Location.Y + TopMargin + (RowCount + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);

g.DrawString(this.send_amount .ToString (), TheDataGrid.Font, ForeBrush, cellbound4, new StringFormat());

RectangleF cellbound5 = new RectangleF(TheDataGrid.Location.X+4*columnwidth,
TheDataGrid.Location.Y + TopMargin + (RowCount + 1) * (TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway),
columnwidth,
TheDataGrid.Font.SizeInPoints + kVerticalCellLeeway);

g.DrawString(this.send_moneys .ToString (), TheDataGrid.Font, ForeBrush, cellbound5, new StringFormat());


//g.DrawString("合计:", TheDataGrid.HeaderFont, ForeBrush, cellbounds, cellformat);
return false;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return false;
}

}

void DrawHorizontalLines(Graphics g, ArrayList lines)
{
Pen TheLinePen = new Pen(TheDataGrid.GridLineColor, 1);

if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
return;

for (int i = 0; i < lines.Count; i++)
{
g.DrawLine(TheLinePen, TheDataGrid.Location.X, (float)lines[i], PageWidth, (float)lines[i]);
}
}

void DrawVerticalGridLines(Graphics g, Pen TheLinePen, int columnwidth, int bottom)
{
if (TheDataGrid.GridLineStyle == DataGridLineStyle.None)
return;

for (int k = 0; k < TheTable.Columns.Count; k++)
{
g.DrawLine(TheLinePen, TheDataGrid.Location.X + k*columnwidth,
TheDataGrid.Location.Y + TopMargin,
TheDataGrid.Location.X + k*columnwidth,
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;
}
}
}
}
搜索更多相关主题的帖子: using datagrid System private int 
2005-08-06 17:05
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
收藏
得分:0 
转贴,忘写了,请原谅!

2005-08-06 17:06
gaofangtao
Rank: 1
等 级:新手上路
帖 子:128
专家分:0
注 册:2005-4-7
收藏
得分:0 
谢谢啦,真是及时雨啊!

我相信我的付出
2005-08-07 07:51
快速回复:关于datagrid的打印
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031928 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved