Multi-function DataGrid print class (WinForm C #)

xiaoxiao2021-03-06  92

The print function as shown above can be implemented.

· All fonts, margins, Header are high, high travel, can be customized.

· Supports automatic calculation of the number of rows per page and fixed lines per page.

· Support footage display page number.

Because you use it with your own, you are set to be public, be careful when you value.

Using system.collections; using system.drawing; using system.drawing.printing; using system.data;

Using system.windows.forms;

namespace cjManager {public class cutePrinter {private DataGrid dataGrid; private PrintDocument printDocument; private PageSetupDialog pageSetupDialog; private PrintPreviewDialog printPreviewDialog;

Private string title = ""; int currentpageindex = 0; int = 0; int pagecount = 0;

INT TITLESIZE = 16; BOOL ISCUSTOMHEADER = FALSE; // brush alertbrush = new solidbrush;

String [] header = null; // If the custom line is filled in the string, if you need to slide, use / indicate, for example: number # name where # is splitchar string [] UPLINEHEADER = NULL; // Upper Group int [] UPLINEHEADERINDEX = NULL; // The text index of the upstream, if there is no uplink, set to -1; // Bool ISEVERYPAGEPRINTHEAD = true; // Do you have a list.

Public Bool ISEVERYPAGEPRINTTILE = false; // Whether each page is printed. Public int headerHeight = 50; // Title height. Public int topMargin = 60; // Top edge distance public int celltopMargin = 6; // cell top edge is scacing public int cellleftmargin = 4; // Cell left side from public char splitchar = '#'; // When Header wants to use When the slash is expressed, public string falsestr = "×"; // If there is false in the DataGrid, the characters are converted. Public String Truestr = "√"; // If there is TRUE in the DataGrid that is passed, it converts its character. Public int PageRowcount = 7; // Variety per page PUBLIC INT ROWGAP = 30; // High public int colgap = 5; // Each column interval public int leftmargin = 50; // Left margin PUBLIC FONT TITLONT = New Font ( "Arial", 14); // Title Font Public Font Font = New Font ("Arial", 10); // Text Font PUBLIC FONT Headerfont = New Font ("Arial", 9, FontStyle.Bold); // Column Name Title Public Font Footerfont = New Font ("Arial", 8); // Font Font Display Tset Font PUBLIC FONT UPLINEFONT = New Font ("Arial", 9, FontStyle.Bold); // When Header is divided into two lines When the display is displayed, the font is displayed. Public font underlinefont = new font ("arial", 8); // When the Header is displayed two lines, the font displayed is displayed. Public brush brush = new solidbrush; // Draw Public Bool ISAUTOPAGEROWCOUNT = true; // Automatically calculates the number of lines. Public int button = 80; // bottom margin public bool needprintpageIndex = true; // Whether to print pages page // String filterstr = "

Public Cuteprinter (DataGrid DataGrid, String Title, INT Titlesize) {this.title = Title; //this.titlesize=titlesize;

THISDATAGRID = DataGrid; printDocument = new printDocument (); printDocument.printpage = new printPageEventHandler (this.PrintDocument_printpage);

} Public cuteprinter (datagrid datagrid, string title) {this.title = Title

this.dataGrid = dataGrid; printDocument = new PrintDocument (); printDocument.PrintPage = new PrintPageEventHandler (this.printDocument_PrintPage);} public cutePrinter (DataGrid dataGrid) {this.dataGrid = dataGrid; printDocument = new PrintDocument (); printDocument.PrintPage = new PrintPageEventHandler (this.printDocument_PrintPage);} public bool setTowLineHeader (string [] upLineHeader, int [] upLineHeaderIndex) {this.uplineHeader = upLineHeader; this.upLineHeaderIndex = upLineHeaderIndex; this.isCustomHeader = true; return true;} public bool SetHeader (String [] header) {this.Header = header; return true;

}

Private void PrintDocument_printpage (Object sender, system.drawing.printing.printpageEventArgs e) {

Int width = e.pagebounds.width; int Height = E.PageBounds.Height;

IF (this.isautopagerowcount) PageRowcount = (int) ((Height-this.topmargin-titlesize-this.Headerfont.Height-this.HeaderHeight-this.buttommargin /this.rowgap);

PageCount = (int); if (rowcount% PageRowcount> 0) PageCount ;

INT Xoffset = (in width-e.graphics.measurestring (this.title, this.titlefont) .width/2); int coLCount = 0; int x = 0; int y = TopMargin; String CellValue = " "

INT StarTrow = CurrentPageIndex * PageRowCount; int endrow = startrow this.pagerowcount

IF (this.currentpageindex == 0 || this.iseveryPageprinttitle) {E.Graphics.drawstring (this.title, titlefont, brush, xoffset, y); y = titlesize;}

Colcount = DataGrid.TablesTyles [0] .gridcolumnstyles.count;

Y = ROWGAP; x = Leftmargin; Drawline (New Point (x, y), new point (x, y currentpagerowcount * RowGap this.HeaderHeight), E.Graphics); // The leftmost vertical line

INT LastIndex = -1; int lastLength = 0; int indexj = -1;

For (int J = 0; j 0) {indexj ; if (this.Header == null | THISHEXER [indexj] == "" "CellValue = DataGrid.tables [0] .gridcolumnstyles [j] .Headertext; else cellvalue = header [indexj];

IF (this.uplineheaderindex [indexj]! = lastindex) {if (Lastlength> 0 && LastIndex> -1) // Start a UPLINE {string Uplinestr = this.uplineHeader [LastIndex]; int Upxoffset = (int) (LastLength-E.Graphics.MeasureString (Uplinestr, this.uplinefont) .width/2); if (UpxOffset <0) Upxoffset = 0; E.Graphics.drawString (Uplinestr, this.uplinefont, Brush, x-la castlength upxoffset, y (int) (this.celltopmargin / 2));

Drawline (X-Lastlength, Y (INT) (3.HeaderHeight / 2)), New Point (this.HeaderHeight / 2)), E.GRAPHICS); / / Middle Line DRAWLINE New Point (X, Y), New Point (this.HeaderHeight / 2)), E.Graphics;} LastIndex = this.uplineHeaderIndex [indexj]; LastLength = COLWIDTH COLGAP;} else { Lastlength = COLWIDTH COLGAP;}}

// int currentY = y cellTopMargin; int Xoffset = 10; int Yoffset = 20; int leftWordIndex = cellValue.IndexOf (this.splitChar); if (this.upLineHeaderIndex = null && this.upLineHeaderIndex [indexj]> - 1!) {if (leftWordIndex> 0) {string leftWord = cellValue.Substring (0, leftWordIndex); string rightWord = cellValue.Substring (leftWordIndex 1, cellValue.Length-leftWordIndex-1); // the above word Xoffset = (int) (colWidth colGap-e.Graphics.MeasureString (rightWord, this.upLineFont) .Width); Yoffset = (int) (this.headerHeight / 2-e.Graphics.MeasureString ( "a", this.underLineFont) .Height) ; // xoffset = 6; // yoffset = 10; E.Graphics.drawstring (Rightword, this.underLineFont, Brush, X Xoffset-4, Y (int)); E.Graphics. DrawString (Leftword, this.underLinefont, Brush, X 2, Y (int) (this.celltopmargin / 2) Yoffset-2); Drawline (New Point (x, y (int)), New Point (X COLWIDTH COLGAP, Y HeaderHeight), E.GRAP Hics); X = COLWIDTH COLGAP; Drawline (X, Y (int)), New Point (x, y currentpagerowcount * RowGap this.HeaderHeight), E.GRAPHICS) Else {

E.Graphics.drawstring (CellValue, Headerfont, Brush, X, Y (int) (this.celltopmargin / 2)); x = COLWIDTH COLGAP; DRAWLINE (New Point X, Y (int)), New Point (x, y currentpageRowcount * RowGap this.HeaderHeight), ELRAPHICS);}} else {if (LeftWordIndex> 0) {string leftword = cellValue.Substring (0, leftWordIndex); string rightWord = cellValue.Substring (leftWordIndex 1, cellValue.Length-leftWordIndex-1); // the above word Xoffset = (int) (colWidth colGap-e.Graphics.MeasureString ( rightWord, this.upLineFont) .Width); Yoffset = (int) (this.headerHeight-e.Graphics.MeasureString ( "a", this.underLineFont) .Height); e.Graphics.DrawString (rightWord, this.headerFont, Brush, x xoffset-4, y 2); E.Graphics.drawstring (Leftword, this.Headerfont, Brush, X 2, Y Yoffset-4); Drawline (New Point (x, y), New Point (X COLWIDTH COLGAP, Y HeaderHeight), E.Graphics; x = COLWIDTH COLGAP; Drawline (New Point (x, y) , New Point (x, y currentPageRowCount * rowGap this.headerHeight), e.Graphics);} else {e.Graphics.DrawString (cellValue, headerFont, brush, x, y cellTopMargin); x = colWidth colGap Drawline (New Point (X, Y), New Point (x, y currentpagerowcount * rowgap this.HeaderHeight), E.Graphics);}}

}}} The loop ends, draws the last UPLINE IF (this.iscustomheader) {if (LastLength> 0 && LastIndex> -1) // starts drawing a UPLINE {String Uplinestr = this.uplineHeader [LastIndex]; int upxoffset = Int (Lastlength-E.Graphics.measureString (Uplinestr, this.uplinefont) .width/2); if (UpxOffset <0) UpxOffset = 0; E.Graphics.drawstring (Uplinestr, this.uplinefont, Brush, X -LastLength UpxOffset, Y (int) (THISCELLTOPMARGIN / 2); Drawline (X-LastLength, Y (int)), New Point (x, y (int) (xi) (xi) (x, y (int) (INT) THISHEADERHEIGHT / 2)), E.Graphics); // Middle Line Drawline (New Point (x, y), new point (x, y (int)), E.GRAPHICS) } Int rightbound = x;

Drawline (New Point (LeftMargin, Y), New Point (Rightbound, Y), E.Graphics; // Topline

// Drawline (New Point (LeftMargin, Y this.HeaderHeight), New Point (Rightbound, Y this.HeaderHeight), E.Graphics; // The following line

Y = this.headerheight;

// Print All Rows for (INT I = Startrow; I 0) {CELLVALUE = DataGrid [i, j] .tostring (); if (cellvalue == "false) CellValue = falsestr; if (CellValue ==" true ") CellValue = TrueString (CellValue, Font, Brush, Y CelltopMargin); x = DataGrid.tables [0] .GridColumnStyles [j] .width colgap; y = y rowgap * (CellValue.Split (New char [] {'/ r', '/n'} ).length - 1);}} Drawline (New Point (LeftMargin, Y), New Point (RightBound, Y), E.Graphics; Y = ROWGAP; Drawline (New Point (LeftMargin, Y), New Point (Rightbound, Y), E.Graphics; CurrentPageIndex ;

IF (this.needprintpageIndex) E.Graphics.drawstring ("Total" PageCount.toTRING () page, current " this.currentpageIndex.tostring () " page ", this.footerfont, brush, width-200 , (int) (Height-this.buttommargin / 2-this.footerfont.Height);

String s = CellValue; String F3 = CellValue;

if (currentPageIndex

Public PrintDocument GetPrintDocument () {Return PrintDocument;

Public void print () {rowcount = 0;

IF (DataGrid.DataSource.gettype (). TOSTRING () == "System.Data.DataTable") {rowcount = ((DataTable) DataGrid.DataSource ).Rows.count;} else if (data " .ToString () == "System.Collections.ArrayList") {rowCount = ((ArrayList) dataGrid.DataSource) .Count;} try {pageSetupDialog = new PageSetupDialog (); pageSetupDialog.Document = printDocument; pageSetupDialog.ShowDialog ();

printPreviewDialog = new PrintPreviewDialog (); printPreviewDialog.Document = printDocument; printPreviewDialog.Height = 600; printPreviewDialog.Width = 800; printPreviewDialog.ShowDialog (); ". Printer error"} catch (Exception e) {throw new Exception ( e. Message);

}}}

// Usage example, the display result is shown in top.

private void bnPrint_Click (object sender, System.EventArgs e) {cutePrinter dgp = new cutePrinter (this.dataGrid1, this.dlSearchYear.Text "in" "professional", 16); string [] uplinestr = { "Oh, hehe" , "Xixi"}; string [] header = {"huh #xixi", "Hee # XCC", "KK # JJ", "KK # JJ", "KDJF", "," ",", " "}; DGP.setHeader (HEADER); // Do not assign a value if you don't have to change the Header. // Note that the column here does not include the column INT [] UPLINEINDEX = {- 1, -1, 0, 0, 0, -1, 1, 1}; // not included here. Width == 0 column DGP.SettowLineHead (Uplinestr, UPLineIndex); DGP.Print ();}

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

New Post(0)