Printing the DataGrid report using the class .NET comes with.

zhaozj2021-02-16  142

Use the .NET itself to implement the print of the DataGrid report.

Using system.windows.forms; using system.drawing; using system.data; using system.collections; using datalibrary;

namespace ControlLibrary {///

/// DataGrid print /// public class DataGridPrinter {private PrintDocument ThePrintDocument; private DataTable TheTable; private DataGrid TheDataGrid; public int RowCount = 0; private const int kVerticalCellLeeway = 10; public int PageNumber = 1; public ArrayList Lines = new ArrayList (); public int header = 0; public int footer = 0; DataCatena DataCatena1; public int PageWidth; public int PageHeight; public int TopMargin; public int BottomMargin; int gridWidth = 0 Public int LEFTMARGIN; Public Int Right Margin; Public Int PAPERNAME; PUBLIC INT PAGECUNT; Private Int rows_page; public bool showfooter;

public int bodylength {get {if (this.PageNumber

public DataGridPrinter (DataGrid aGrid, PrintDocument aPrintDocument, string theform, int Header, int Footer) {// // TODO: Add constructor logic here // this.header = Header; this.footer = Footer; this.showfooter = true; TheDataGrid = aGrid; ThePrintDocument = aPrintDocument; if (theform = "!") {DataCatena1 = new DataCatena (); DataView myDV = DataCatena1.GetDataView ( "select * from yh_bbdy where bm = '" theform "'"); this. Papername = MYDV [0] ["zm"]. TOSTRING ();

Papersize Pksize;

IF (MYDV [0] ["fx"]. TOSTRING () == "true") this.TheprintDocument.defaultpagesettings.landscape = true; else this.theprintDocument.defaultpagesettings.landscape = false;

int found = 0; for (int i = 0; i

IF (true (this.theprintDocument.defaultpagesettings.landscape) {pageheight = int32.parse (MyDV [0] ["zk"]. TOSTRING ()); PageWidth = Int32.Parse (MyDV [0] [ZC "].} else {pagewidth = int32.parse (MyDV [0] [" zk "]. Tostring ()); PageHeight = Int32.Parse (MyDV [0] [" zc "] .Tostring ());}} TopMargin = int32.parse (MyDV [0] ["SK"]. ToString ()); bottommargin = int32.parse (MyDV [0] ["xk"]. TOSTRING ()); Rightmargin = int32.parse (MyDV [0] ["Rightk"]. ToString ()); LeftMargin = Int32.Parse (MyDV [0] ["Leftk"]. Tostring ());} else {pagewidth = theprintdocument.defaultpagesettings .PaperSize.Width; PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height; TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top; BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom; RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right; LeftMargin = ThePrintDocument.DefaultPageSettings .MARGINS.LEFT;}

PageWidth - = this.Leftmargin; PageWidth - = this.rightmargin;

THIS.ROWS_PAGE = ("this.pageheight-this.topmargin-this.bottommargin-this.footer-this.header) / (theDataGrid.font.sizeinpoints kverticalcellleeway);

for (int k = 0; k

}

public void DrawHeader (Graphics g) {SolidBrush ForeBrush = new SolidBrush (TheDataGrid.HeaderForeColor); SolidBrush BackBrush = new SolidBrush (Color.White); //(TheDataGrid.HeaderBackColor); Pen TheLinePen = new Pen (TheDataGrid.GridLineColor, 1) ; StringFormat cellformat = new StringFormat (); cellformat.Trimming = StringTrimming.EllipsisCharacter; cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit; cellformat.Alignment = StringAlignment.Center; int columnwidth = 0;

INT INTIALROWCOUNT = ROWCOUNT;

// Draw the table header float startXposition = this.leftmargin; //thedatagrid.location.x; Rectanglef nextcellbounds = new Rectanglef (0, 0, 0, 0);

/ * This section is the code for drawing the top color

Rectanglef HeaderBounds = New Rectanglef (0, 0, 0, 0);

HeaderBounds.X = this.LeftMargin; //TheDataGrid.Location.X; HeaderBounds.Y = (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints kVerticalCellLeeway); HeaderBounds.Height = TheDataGrid.Font.SizeInPoints kVerticalCellLeeway; HeaderBounds .Width = PageWidth; * / float y1 = (float) (Header this.topmargin);

g.DrawLine (TheLinePen, this.LeftMargin, y1, this.LeftMargin this.PageWidth, y1); y1 = y1 (float) (TheDataGrid.Font.SizeInPoints kVerticalCellLeeway); g.DrawLine (TheLinePen, this.LeftMargin, y1 , this.Leftmargin this.pageWidth, Y1);

// g.FillRectangle (Backbrush, HeaderBounds);

for (int k = 0; k

string nextcolumn = TheDataGrid.TableStyles [0] .GridColumnStyles [k] .HeaderText; // TheTable.Columns [k] .ToString (); (RowCount - initialRowCount) * (TheDataGrid.Font.SizeInPoints kVerticalCellLeeway) RectangleF cellbounds = new RectangleF (startxposition, header TopMargin 2, columnwidth, TheDataGrid.HeaderFont.SizeInPoints kVerticalCellLeeway-2); nextcellbounds = cellbounds; if (startxposition columnwidth <= PageWidth this.LeftMargin) {g.DrawString (nextcolumn, TheDataGrid.HeaderFont Forebrush, cellbounds, cellformat;

STARTXPSITION = STARTXPOSITION ColumnWidth;

} Y1 = y1 (float) (TheDataGrid.Font.SizeInPoints kVerticalCellLeeway); g.DrawLine (TheLinePen, this.LeftMargin, y1, this.LeftMargin this.PageWidth, y1);! // if (TheDataGrid.GridLineStyle = DataGridLineStyle .None) // g.drawline (theLinepen, THEDATAGRID.LOCATION.X, NextCellbounds.bottom, PageWidth, Nextcellbounds.Bottom);

Public void setDataSource (DataTable Atable) {THHISABLE = ATABLE; this.pageCount = (int) Math.ceiling (THIS.ROWS.COUNT) / (THIS.ROWS_PAGE)); // MessageBox.show "ROWS_COUNT:" THHETABLE.ROWS.COUNT.TOSTRING () "ROWS_PAGE:" ROWS_PAGE.TOSTRING () "Page_count:" this.pagecount.tostring ()); // this.theprintDocument}

Public Bool DrawROWS (GRAPHICS G) {INT LASTROWBOTTOM = TopMargin; LINES.CLEAR ();

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 / TheDataGrid.TableStyles [0] .GridColumnStyles.Count; int initialRowCount = (this.PageNumber- 1) * this.rows_page; int endrow = initialroWCount this.rows_page;

Rectanglef Rowbounds = New Rectanglef (0, 0, 0, 0);

// DRAW VERTICAL LINES

// Draw the rows of the table for for (INT i = I ) {DATAROW DR = THETABLE.ROWS [I]; int startXposition = this.leftmargin 2; / / The-etiTAGrid.location.x;

RowBounds.X = 0; RowBounds.Y = header TopMargin ((RowCount - initialRowCount) 1) * (TheDataGrid.Font.SizeInPoints kVerticalCellLeeway); RowBounds.Height = TheDataGrid.Font.SizeInPoints kVerticalCellLeeway; RowBounds.Width = PageWidth;

/ * /// This section is the code IF (I% 2 == 0) of the drawing table, {g.fillRectangle (backbrush, rowbounds);} else {g.fillRectangle (alternatingbackbrush, rowbounds);} * /

for (int j = 0; j

STARTXPSITION = STARTXPSITION ColumnWidth 2;}

Lines.Add (Rowbounds.Bottom); Lastrowbottom = (int) Rowbounds.Bottom;

Rowcount ;}

Drawhorzontallines (G, Lines); DrawvertageGridLines (G, THELINEPEN, LASTROWBOTTOM); if (this.showfooter == true) DrawFooter (G);

IF (THIS.PAGENUMBER

} Catch (exception ex) {messagebox.show (ex.Message.toString ());

Return false;}

}

Void Drawfooter (Graphics G) {FontFamily Fontfamily = New FontFamily ("Song");

String text = "第" this.pagenumber.tostring () "page total" this.pageCount.toString () "page";

Font font = new font (FontFamily, 10, FontStyle.Regular, GraphicsUnit.Point);

Sizef size = g.MeasureString (Text, font);

Int Startpos = this.pageheight - height - height - height - height - height - height - height - height - height - height;

Solidbrush Forebrush = New Solidbrush (Color.Black);

StringFormat cellFormat = new stringformat (); g.drawstring (TEXT, FONT, Forebrush, (this.pagewidth-size.width) / 2 this.leftmargin, StartPos, CellFormat;

Text = "[cobainsoft]";

Size = g.measureString (Text, Font);

g.drawstring (Text, Font, Forebrush, this.pagewidth this.leftmargin - size.width-3, startpos, cellformat);

}

Void Drawhorizontallines (Graphics G, ArrayList Lines) {Pen Thelinepen = New Pen (TheDataGrid.gridlineColor, 1);

IF (theDataGrid.gridLinesTyle == DataGridLineStyle.none) return;

INT I;

For (i = 0; i

}

Void DrawVerticalGridLines (graphics g, pen thelinepen, int bottom) {if (theDataGrid.gridLineStyle == DataGridLinesTyle.none) Return;

INT POSX = 0;

/ / Draw the leftmost vertical line

G. Drawline (TheLinepen, this.Leftmargin POSX, Header TopMargin, this.Left Margin POSX, BOTTOM);

// Pain the rightmost vertical line g.drawline (TheLinepen, this.Leftmargin this.Pagewidth, Header TopMargin, this.pageWidth, Bottom);

INT TOTAL = theseDataGrid.tablesTyles [0] .gridColumnStyles.count -1; for (int K = 0; k

Public Bool DrawDataGrid (Graphics G) {

Try {DrawHeader (G); Bool BContinue = DrawROWROWS (G); Return BContinue;} catch (exception ex) {messagebox.show (ex.Message.toString ()); return false;}}

}

}

Posted on August 4, 2004 12:10 am

转载请注明原文地址:https://www.9cbs.com/read-9064.html

New Post(0)