关于datagrid的打印

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

}

}

}

时间: 2024-07-30 07:41:12

关于datagrid的打印的相关文章

提供一个DataGrid的打印类

namespace AsterDnet{    using System;    using System.ComponentModel;    using System.Windows.Forms;    using System.Drawing;    using System.Drawing.Printing;    using System.IO;    using System.Data ;    using System.Data.SqlClient ;        /// <su

一个通用的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

求数据库显示在datagrid例子并打印出datagrid内容.急急急!!!

问题描述 是想把这句"selectsum(a),sum(b),sum(C)fromtables1whereworkdate='20071108'andbrnoin(selectbrnofromtables2wherembrnoin('4000','4010'))andcurrtype='001'andsubnolike'i%'"中的sum(a),sum(b),sum(c)所求得的值显示在datagrid里面并可以打印出来简单的数据库显示在datagrid里面和把datagrid的打印~

C# WinForm开发系列 - DataGrid/DataGridView

在WinForm开发中,DataGrid/DataGridView被广泛使用于绑定数据库中数据进行呈现.整理一些关于DataGrid /DataGridView使用的文章,涉及DataGrid/DataGridView基本功能,自定义绘制控件,数据导入/导出(Excel),打印 等. 1.新数据网格简介(DataGridView) 2.自定义DataGridView外观 3.怎样让WinForms下DataGrid可以像ASP.NET下的DataGrid一样使用自定义的模板列 4.在Winfor

使用.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 c

多功能DataGrid打印类(WinForm C#)

datagrid|打印 能实现如上图的的打印功能. ·所有字体,边距,header 高,行高,都可以自定义. ·支持自动计算每页行数与每页固定行数. ·支持页脚显示页数. 由于自己用和本人比较懒,所以把属性都设置成公有,赋值的时候小心. using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Drawing.Printing;using System.D

WPF使用FixedDocument打印DataGrid的问题

问题描述 我在WPF程序中使用PrintDialog进行打印FixedDocument,FixedDocument里面有个DataGrid,打印前设置了数据源,在win8下直接打印(不经过xps预览)很正常,但是换到windowsxp上,打印出的DataGrid里没有数据,并且若先使用xps文件和DocumentViewer进行打印预览,再使用DocumentViewer上的打印按钮打印,也没问题.正常情况:异常情况: 解决方案 解决方案二:和我遇到的问题一样,我是在win8上可以打印控件,但到

跪求在.NET2003里怎样实现DataGrid水晶报表打印 WEB程序的?在线等!望高手速度解决!谢谢!急!!!!!

问题描述 希望大家发展点同情心好么! 解决方案 解决方案二:if(mytable.Rows.Count==0)//mytable是一个DataTable,也可用DataSet{this.Button1.Enabled=false;return;}System.Web.UI.WebControls.DataGriddgExport=null;//IO用于导出并返回excel文件System.IO.StringWriterstrWriter=null;System.Web.UI.HtmlTextWr

C# datagrid打印问题

问题描述 在datagrid里面显示的数据,我现在要打印出来,请问大家有没有好的控件,如果没有,怎么去实现呢?急,麻烦大家速回. 解决方案 解决方案二:难道大家一定要有分才回答?失望.