日期:2008-04-06  浏览次数:20366 次

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,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 &q