轩尼诗xo酒700价格表:一个关于DataGrid的打印类,分享,感谢作者 - 活靶子.Net - 博客园

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 10:24:56
手中的活计需要打印,就到处找找。
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);}}}}

一个关于DataGrid的打印类,分享,感谢作者 - 活靶子.Net - 博客园 DataGrid使用总结 - 小云子、asp.net开发学习笔记 - CSDN博客 .NET开发十大常用工具软件分享 - 钱途无梁 - 博客园 datagrid\mshflexgrid\msflexgrid的区别 纯脚本搞掂DataGrid表表头不动,表身滚动。 - Think - 博客园 .NET中反射机制的使用与分析 - focus on .NET - 博客园 DataGrid控件的列宽自动填充 破译随机组合的规律,验证!感谢作者。 .NET知识结构 - 而立的日志 - 网易博客 .Net Petshop详解[续] - torome - 博客园 为你的悔改归正感谢神(巴刻) 求知论坛 - powered by phpwind.net 关于打印机会出现“该文档未能打印”的解决方案思路 [分享] 关于编程的学习 给天下母亲的一封信---关于教育孩子 打印 订阅 刷新 分享我的书架:一个五年程序员的学习观 - 常高伟的专栏 - CSDN博客 老徐的博客 ? Blog Archive ? .NET Framework 4 类库开发的设计准则 使DataGrid控件的单元格具有下拉功能 AS3自定义Datagrid控件的样式(以下举例字体样式) 关于聪明与智慧的箴言-bokee.net 关于Memcache mutex设计模式的.net实现 IE6 IE7 FF的CSS Hack总结(转载) - john23.net - 博客园 [你必须知道的.NET]第十九回:对象创建始末(下) - Anytao - 博客园 .NET中反射机制的使用与分析 - pk3000lc - 博客园 [分享]一个馒头引发的血案 - 人生之路 管理起步 - 畅享博客