打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
一个关于DataGrid的打印类,分享,感谢作者 - 活靶子.Net - 博客园
手中的活计需要打印,就到处找找。
google中扑获,挺不错的,大家共享~
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 dataGrid;private PrintDocument printDocument;private PageSetupDialog pageSetupDialog;private PrintPreviewDialog printPreviewDialog;public DataGridPrinter(DataGrid 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 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 headingsy += 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);}}}}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
winform 自带打印控件(转)
C#打印操作——打印设置、打印预览及打
全面解析C#实现打印功能
打印功能及打印预览实现(C#
asp.net如何连接MySQL
高分求解vb.net 打印的问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服