eg:
private datagridprinter m_odatagridprinter;
protected system.windows.forms.printpreviewdialog printpreviewdialog1;
private system.windows.forms.printdialog printdialog1;
public system.drawing.printing.printdocument printdocument1;
// 预览
try
{
m_odatagridprinter = new datagridprinter(this.datagrid_brand, printdocument1, (datatable)this.datagrid_brand.datasource);
this.printpreviewdialog1.showdialog();
catch
{
messagebox.show("没有找到打印机,不能预览!");
}
// 打印
public dialogresult print()
{
try
{
// datatable odttmp = (datatable)this.datagrid_brand.datasource;
// if (odttmp==null || odttmp.rows.count<=0)
// {
// messagebox.show("主窗口中没有数据,请进行/"查询/"或者/"显示所有/"等操作,将您要打印的数据显示在主窗口中,然后再进行打印!");
// return dialogresult.cancel;
// }
m_odatagridprinter = new datagridprinter(this.datagrid_brand, printdocument1, (datatable)this.datagrid_brand.datasource);
if (this.printdialog1.showdialog() == dialogresult.ok)
{
this.printdocument1.print();
return dialogresult.ok;
}
else
{
return dialogresult.cancel;
}
}
catch
{
messagebox.show("没有找到打印机,不能打印!");
return dialogresult.cancel;
}
return dialogresult.ok;
}
// 事件定义
this.printdocument1.documentname = "brand";
this.printdocument1.printpage += new system.drawing.printing.printpageeventhandler(this.printdocument1_printpage);
// 事件响应
private void printdocument1_printpage(object sender, system.drawing.printing.printpageeventargs e)
{
e.hasmorepages = m_odatagridprinter.drawdatagrid(e.graphics);
if (e.hasmorepages)
{
m_odatagridprinter.pagenumber += 1;
}
else
{
m_odatagridprinter.pagenumber = 1;
m_odatagridprinter.rowcount = 0;
}
// 基类定义
imports system.drawing.printing
imports system.windows.forms
imports system.drawing
public class datagridprinter
public rowcount as integer = 0
public pagenumber as integer = 1
private m_datatable as datatable
private m_datagrid as datagrid
private m_imagearray(2) as image
private m_pagewidth as integer
private m_pagewidthminusmargins as integer
private m_pageheight as integer
private m_adjcolumnby as integer
private m_istoowide as boolean
private m_datagridwidth as integer
private m_sselectstring as string
private const c_topmargin as integer = 50
private const c_bottommargin as integer = 50
private const c_leftmargin as integer = 50
private const c_rightmargin as integer = 50
private const c_verticalcellleeway as integer = 10
public sub new(byval dg as datagrid, byval pd as printdocument, byval dt as datatable)
m_sselectstring = ""
m_datagrid = dg
m_datatable = dt
'set the document as landscape
pd.defaultpagesettings.landscape = true
'extract our width and height values
m_pageheight = pd.defaultpagesettings.papersize.width
m_pagewidth = pd.defaultpagesettings.papersize.height
m_pagewidthminusmargins = m_pagewidth - (c_leftmargin + c_rightmargin)
'hard-coded images
'm_imagearray(0) = image.fromfile("images/major.gif")
'm_imagearray(1) = image.fromfile("images/medium.gif")
'm_imagearray(2) = image.fromfile("images/minor.gif")
m_datagridwidth = getdatagridwidth()
'set up some adjustments to scale the output later
if m_datagrid.width > m_pagewidthminusmargins then
m_adjcolumnby = m_datagrid.width - m_pagewidthminusmargins
m_datagridwidth = m_datagridwidth - m_adjcolumnby
m_istoowide = true
else
m_adjcolumnby = m_pagewidthminusmargins - m_datagrid.width
m_datagridwidth = m_datagridwidth + m_adjcolumnby
m_istoowide = false
end if
end sub
public sub new(byval dg as datagrid, byval pd as printdocument, byval dt as datatable, byval headstring as string)
m_sselectstring = headstring
m_datagrid = dg
m_datatable = dt
'set the document as landscape
pd.defaultpagesettings.landscape = true
'extract our width and height values
m_pageheight = pd.defaultpagesettings.papersize.width
m_pagewidth = pd.defaultpagesettings.papersize.height
m_pagewidthminusmargins = m_pagewidth - (c_leftmargin + c_rightmargin)
'hard-coded images
'm_imagearray(0) = image.fromfile("images/major.gif")
'm_imagearray(1) = image.fromfile("images/medium.gif")
'm_imagearray(2) = image.fromfile("images/minor.gif")
m_datagridwidth = getdatagridwidth()
'set up some adjustments to scale the output later
if m_datagrid.width > m_pagewidthminusmargins then
m_adjcolumnby = m_datagrid.width - m_pagewidthminusmargins
m_datagridwidth = m_datagridwidth - m_adjcolumnby
m_istoowide = true
else
m_adjcolumnby = m_pagewidthminusmargins - m_datagrid.width
m_datagridwidth = m_datagridwidth + m_adjcolumnby
m_istoowide = false
end if
end sub
public function drawdatagrid(byval g as graphics) as boolean
try
drawpageheader(g)
return drawpagerows(g)
catch ex as exception
' messagebox.show(ex.message.tostring())
return false
end try
end function
private sub drawpageheader(byval g as graphics)
'use this format when drawing later
dim cellformat as new stringformat
cellformat.trimming = stringtrimming.word
cellformat.formatflags = stringformatflags.nowrap or stringformatflags.linelimit
if m_sselectstring.length > 0 then
'temp width to draw this column
dim columnwidth as integer = m_pagewidthminusmargins - 10
'create a layout rectangle to draw within.
dim cellbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 9, columnwidth, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)
g.drawstring(m_sselectstring, m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
'create the header rectangle
'dim headerbounds as new rectanglef(c_leftmargin, c_topmargin, m_pagewidthminusmargins, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
dim headerbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 3 - 9 - 9 + m_datagrid.preferredrowheight + c_verticalcellleeway, m_pagewidthminusmargins - 10, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)
'draw the header rectangle
g.fillrectangle(new solidbrush(m_datagrid.headerbackcolor), headerbounds)
else
'create the header rectangle
'dim headerbounds as new rectanglef(c_leftmargin, c_topmargin, m_pagewidthminusmargins, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
dim headerbounds as new rectanglef(c_leftmargin + 12, c_topmargin - 9, m_pagewidthminusmargins - 10, m_datagrid.preferredrowheight + c_verticalcellleeway - 3)
'draw the header rectangle
g.fillrectangle(new solidbrush(m_datagrid.headerbackcolor), headerbounds)
end if
dim xposition as single = c_leftmargin + 12 ' +12 for some padding
'find the column names from the tablestyle
dim cs as datagridcolumnstyle
for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
if cs.width > 0 then
'temp width to draw this column
dim columnwidth as integer = cs.width
'scale the summary column width
'note: just a quick way to fit the text to the page width
'this is not the best way to do this but it handles the most
'common ui path for this demo app
if cs.mappingname = "tasksummary" and m_istoowide then
columnwidth -= m_adjcolumnby
elseif cs.mappingname = "tasksummary" then
columnwidth += m_adjcolumnby
end if
if m_sselectstring.length > 0 then
'create a layout rectangle to draw within.
dim cellbounds as new rectanglef(xposition, c_topmargin - 9 + m_datagrid.preferredrowheight + c_verticalcellleeway - 3, columnwidth, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
'draw the column name
g.drawstring(cs.headertext, m_datagrid.headerfont, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
else
'create a layout rectangle to draw within.
dim cellbounds as new rectanglef(xposition, c_topmargin, columnwidth, m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway)
'draw the column name
g.drawstring(cs.headertext, m_datagrid.headerfont, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
end if
'adjust the next x pos
xposition += columnwidth
end if
next
end sub
private function drawpagerows(byval g as graphics) as boolean
'dim yposition as single = c_topmargin + m_datagrid.headerfont.sizeinpoints + (c_verticalcellleeway * 2)
dim yposition as single = c_topmargin + m_datagrid.preferredrowheight
if m_sselectstring.length > 0 then
yposition += m_datagrid.preferredrowheight
end if
'use this format when drawing later
dim cellformat as new stringformat
cellformat.trimming = stringtrimming.word
cellformat.formatflags = stringformatflags.nowrap or stringformatflags.linelimit
try
if m_datatable.defaultview.count <= 0 then
dim xposition as single = c_leftmargin + 12 ' +12 for some padding
dim cellbounds as new rectanglef(xposition, yposition, c_leftmargin - c_rightmargin, m_datagrid.preferredrowheight)
g.drawstring("对不起,没有找到您需要打印的数据,请您确认程序主窗口中有您需要打印的数据!", m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
end if
catch ex as exception
dim xposition as single = c_leftmargin + 12 ' +12 for some padding
dim cellbounds as new rectanglef(xposition, yposition, c_leftmargin - c_rightmargin, m_datagrid.preferredrowheight)
g.drawstring("对不起,没有找到您需要打印的数据,请您确认程序主窗口中有您需要打印的数据!", m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
return false
end try
'loop each visible row
dim i as integer = 0
for i = rowcount to (m_datatable.defaultview.count - 1)
dim xposition as single = c_leftmargin + 12 ' +12 for some padding
'g.drawline(new pen(m_datagrid.gridlinecolor, 1), xposition - 3, yposition - 3, xposition - 3, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)
'loop the columns of this row, if the column is visible
'then print the cell value
dim cs as datagridcolumnstyle
for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
if cs.width > 0 then
'temp width to draw this column
dim columnwidth as integer = cs.width
'scale the summary column width
'note: just a quick way to fit the text to the page width
'this is not the best way to do this but it handles the most
'common ui path for this demo app
if cs.mappingname = "tasksummary" and m_istoowide then
columnwidth -= m_adjcolumnby
elseif cs.mappingname = "tasksummary" then
columnwidth += m_adjcolumnby
end if
'create a layout rectangle to draw within.
'dim cellbounds as new rectanglef(xposition, yposition, columnwidth, m_datagrid.font.sizeinpoints + c_verticalcellleeway)
dim cellbounds as new rectanglef(xposition, yposition, columnwidth, m_datagrid.preferredrowheight)
'draw the item value
if cs.mappingname = "商标图片" then
try
'draw image
dim by as byte() = ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte())
dim imagetodraw as image
dim omemorystream as system.io.memorystream
omemorystream = new system.io.memorystream(by, 0, by.length)
'omemorystream = new system.io.memorystream((byte[])this.getcolumnvalueatrow(source, rownum), 0, ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte))
'omemorystream = new system.io.memorystream(ctype(m_datatable.defaultview.item(i).item(cs.mappingname), [] byte), 0, ctype(m_datatable.defaultview.item(i).item(cs.mappingname), byte))
'memorystream omemorystream=new memorystream((byte[])this.getcolumnvalueatrow(source, rownum),0,((byte[])this.getcolumnvalueatrow(source, rownum)).length);
imagetodraw = system.drawing.image.fromstream(omemorystream)
g.drawimage(imagetodraw, new rectangle(xposition, yposition, 125, 36))
'select case m_datatable.defaultview.item(i).item("prioritytext")
' case "major"
' g.drawimage(m_imagearray(0), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
' case "medium"
' g.drawimage(m_imagearray(1), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
' case "minor"
' g.drawimage(m_imagearray(2), new point(convert.toint32(cellbounds.x) - 5, convert.toint32(cellbounds.y)))
'end select
catch ex as exception
end try
else
'draw as short date format or regular string
'if m_datatable.defaultview.item(i).item(cs.mappingname).gettype() is gettype(datetime) then
' g.drawstring(string.format("{0:d}", m_datatable.defaultview.item(i).item(cs.mappingname)), m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
'else
cellbounds.y = cellbounds.y + 8
'cellbounds.height = cellbounds.height - 8
try
g.drawstring(ctype(m_datatable.defaultview.item(i).item(cs.mappingname), string), m_datagrid.font, new solidbrush(m_datagrid.headerforecolor), cellbounds, cellformat)
'end if
catch ex as exception
end try
cellbounds.y = cellbounds.y - 8
'cellbounds.height = cellbounds.height + 8
end if
g.drawline(new pen(m_datagrid.gridlinecolor, 1), xposition, yposition - 4, xposition, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)
'adjust the next x pos
xposition += columnwidth
end if
next
'set the rowcount (which is used for a possible next page)
rowcount += 1
g.drawline(new pen(m_datagrid.gridlinecolor, 1), m_pagewidthminusmargins + c_leftmargin - 1, yposition - 4, m_pagewidthminusmargins + c_leftmargin - 1, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 1)
'finished with this row, draw a nice line
g.drawline(new pen(m_datagrid.gridlinecolor, 1), c_leftmargin + 12, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 2, m_pagewidthminusmargins + c_leftmargin, yposition + m_datagrid.preferredrowheight + c_verticalcellleeway - 2)
'adjust the next y pos
'yposition += m_datagrid.headerfont.sizeinpoints + c_verticalcellleeway + 3
yposition += m_datagrid.preferredrowheight + c_verticalcellleeway + 3
'if at end of page exit out for next page
if yposition * pagenumber > pagenumber * (m_pageheight - (c_bottommargin + c_topmargin)) then
return true
end if
next
return false
end function
private function getdatagridwidth() as integer
try
dim cs as datagridcolumnstyle
dim dgwidth as integer = 0
for each cs in m_datagrid.tablestyles(0).gridcolumnstyles
if cs.width <> 0 then
dgwidth = dgwidth + cs.width
end if
next
return dgwidth
catch ex as exception
throw ex
end try
end function
end class