A print class for DataGrid. Thank you for sharing it.

Source: Internet
Author: User
You need to print the task in your hand.
It's good to win from Google. You can share it with me ~
How can I print the data in datagrid?
Http://forums.aspfree.com/archive/t-17107
Http://forums.aspfree.com/t17107/s.html
Using system; using system. collections; using system. componentmodel; using system. drawing; using system. drawing. printing; using system. data; using system. windows. forms; namespace hooooo. print {public class datagridprinter {private DataGrid; private printdocument; private pagesetupdialog; private printpreviewdialog; Public datagridprinter (DataGrid) {This. dataGrid = DataGrid; printdocument = new printdocument (); printdocument. printpage + = new printpageeventhandler (this. printdocument_printpage);} private void printdocument_printpage (Object sender, system. drawing. printing. printpageeventargs e) {int rowcount = 0; int colcount = 0; int x = 0; int y = 0; int rowgap = 20; int colgap = 5; int leftmargin = 50; font font = new font ("Arial", 10); font headingfont = new font ("Arial", 11, fontstyle. underline); font captionfont = new font ("Arial", 10, fontstyle. bold); brush = new solidbrush (color. black); string cellvalue = ""; if (DataGrid. datasource. getType (). tostring () = "system. data. datatable ") {rowcount = (datatable) DataGrid. datasource ). rows. count;} else if (DataGrid. datasource. getType (). tostring () = "system. collections. arraylist ") {rowcount = (arraylist) DataGrid. datasource ). count;} colcount = DataGrid. tablestyles [0]. gridcolumnstyles. count; // print captionif (DataGrid. captionvisible) {Y + = rowgap; X = leftmargin; E. graphics. drawstring (DataGrid. captiontext, captionfont, brush, x, y);} // print headings y + = rowgap; X = leftmargin; For (Int J = 0; j <colcount; j ++) {If (DataGrid. tablestyles [0]. gridcolumnstyles [J]. width> 0) {cellvalue = DataGrid. tablestyles [0]. gridcolumnstyles [J]. headertext; E. graphics. drawstring (cellvalue, headingfont, brush, x, y); x + = DataGrid. tablestyles [0]. gridcolumnstyles [J]. width + colgap ;}// print all rowsfor (INT I = 0; I <rowcount; I ++) {Y + = rowgap; X = leftmargin; for (Int J = 0; j <colcount; j ++) {If (DataGrid. tablestyles [0]. gridcolumnstyles [J]. width> 0) {cellvalue = DataGrid [I, j]. tostring (); E. graphics. drawstring (cellvalue, Font, brush, x, y); x + = DataGrid. tablestyles [0]. gridcolumnstyles [J]. width + colgap; y = Y + rowgap * (cellvalue. split (New char [] {'\ R',' \ n '}). length-1) ;}}string S = cellvalue; string F3 = cellvalue;} public printdocument getprintdocument () {return printdocument;} public void print () {try {pagesetupdialog = new pagesetupdialog (); pagesetupdialog. document = printdocument; pagesetupdialog. showdialog (); printpreviewdialog = new printpreviewdialog (); printpreviewdialog. document = printdocument; printpreviewdialog. height = 600; printpreviewdialog. width = 800; printpreviewdialog. showdialog ();} catch (exception e) {Throw new exception ("Printer error. "+ E. message );}}}}

If you want to print the lines and pages of a table, you can read the following section.Article:
 How to print a DataGrid in C # And. net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.