现在的位置: 首页 > 综合 > 正文

关于datagrid的打印

2012年01月28日 ⁄ 综合 ⁄ 共 8544字 ⁄ 字号 评论关闭
// 关于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)
{
//
// TODO: 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)
{
//
// TODO: 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,000);

RectangleF HeaderBounds = new RectangleF(0000);

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(0000);

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

}

}

}

抱歉!评论已关闭.