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