DataGridView完整实现打印功能

DataGridView实现打印功能
  1 using System;
  2 using System.Drawing;
  3 using System.Collections;
  4 using System.ComponentModel;
  5 using System.Windows.Forms;
  6 using System.Data;
  7 
  8 namespace DataGridPrints
  9 {
 10     /// <summary>
 11     /// Form1 的摘要说明。
 12     /// </summary>
 13     public class Form1 : System.Windows.Forms.Form
 14     {
 15         private System.Windows.Forms.Button button1;
 16         private System.Windows.Forms.DataGrid dataGrid1;
 17         /// <summary>
 18         /// 必需的设计器变量。
 19         /// </summary>
 20         private System.ComponentModel.Container components = null;
 21 
 22         public Form1()
 23         {
 24             //
 25             // Windows 窗体设计器支持所必需的
 26             //
 27             InitializeComponent();
 28 
 29             //
 30             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 31             //
 32         }
 33 
 34         /// <summary>
 35         /// 清理所有正在使用的资源。
 36         /// </summary>
 37         protected override void Dispose( bool disposing )
 38         {
 39             if( disposing )
 40             {
 41                 if (components != null) 
 42                 {
 43                     components.Dispose();
 44                 }
 45             }
 46             base.Dispose( disposing );
 47         }
 48 
 49         #region Windows 窗体设计器生成的代码
 50         /// <summary>
 51         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 52         /// 此方法的内容。
 53         /// </summary>
 54         private void InitializeComponent()
 55         {
 56             this.button1 = new System.Windows.Forms.Button();
 57             this.dataGrid1 = new System.Windows.Forms.DataGrid();
 58             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
 59             this.SuspendLayout();
 60             // 
 61             // button1
 62             // 
 63             this.button1.Location = new System.Drawing.Point(58, 190);
 64             this.button1.Name = "button1";
 65             this.button1.Size = new System.Drawing.Size(90, 24);
 66             this.button1.TabIndex = 0;
 67             this.button1.Text = "Print";
 68             this.button1.Click += new System.EventHandler(this.button1_Click);
 69             // 
 70             // dataGrid1
 71             // 
 72             this.dataGrid1.DataMember = "";
 73             this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
 74             this.dataGrid1.Location = new System.Drawing.Point(38, 43);
 75             this.dataGrid1.Name = "dataGrid1";
 76             this.dataGrid1.Size = new System.Drawing.Size(624, 121);
 77             this.dataGrid1.TabIndex = 1;
 78             // 
 79             // Form1
 80             // 
 81             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
 82             this.ClientSize = new System.Drawing.Size(678, 303);
 83             this.Controls.Add(this.dataGrid1);
 84             this.Controls.Add(this.button1);
 85             this.Name = "Form1";
 86             this.Text = "Form1";
 87             this.Load += new System.EventHandler(this.Form1_Load);
 88             ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
 89             this.ResumeLayout(false);
 90 
 91         }
 92         #endregion
 93 
 94         /// <summary>
 95         /// 应用程序的主入口点。
 96         /// </summary>
 97         [STAThread]
 98         static void Main() 
 99         {
100             Application.Run(new Form1());
101         }
102 
103         private void Form1_Load(object sender, System.EventArgs e)
104         {
105             DataTable dt = SomeDataTable();
106             this.dataGrid1.DataSource = dt;
107             CreatStyle(this.dataGrid1);
108         }
109 
110         private void CreatStyle(DataGrid grid) 
111         { 
112             DataGridTableStyle style = new DataGridTableStyle();
113             DataTable dt = (DataTable) grid.DataSource;
114             style.MappingName = dt.TableName;
115             
116             for(int j = 0; j < dt.Columns.Count; j++) 
117             { 
118                 DataGridTextBoxColumn cs = new DataGridTextBoxColumn(); 
119                 cs.MappingName = dt.Columns[j].ColumnName; 
120                 cs.HeaderText = dt.Columns[j].ColumnName;
121                 cs.ReadOnly = true;
122                 style.GridColumnStyles.Add(cs); 
123             } 
124             grid.TableStyles.Clear(); 
125             grid.TableStyles.Add(style); 
126 
127         } 
128         private DataTable SomeDataTable() 
129         { 
130             
131             DataTable dt = new DataTable("MyTable"); 
132 
133             //add some columns 
134             int nCols = 5; 
135             for(int j = 0; j < nCols; ++j) 
136                 dt.Columns.Add(new DataColumn(string.Format("col{0}", j), typeof(string))); 
137             
138             //add some rows 
139             int nRows = 40; 
140             for(int i = 0; i < nRows; ++i) 
141             { 
142                 DataRow dr = dt.NewRow(); 
143                 for(int j = 0; j < nCols; ++j) 
144                     dr[j] = string.Format("row {0} col {1}", i, j); 
145                 dt.Rows.Add(dr); 
146             } 
147 
148             dt.DefaultView.AllowNew = false;//turn off append row 
149             return dt; 
150         }
151 
152         private void button1_Click(object sender, System.EventArgs e)
153         {
154             GridPrinter dgp=new GridPrinter(this.dataGrid1,0); 
155             string[] header={"AAAA","BBBB","CCCC","DDDD","EEEE"};
156             dgp.setHeader(header);//如果不用改原header就不用赋值。 
157             int[] colWidth = {20,20,20,20,20};
158             dgp.setColWidth(colWidth);
159             
160             int[] disType ={0,0,0,0,0,0};//设定Decimal打印类型,0代表左对齐
161             dgp.setDisplayType(disType);
162             
163             int[] titleDis = {0,0,0,0,0};//设置列标题对齐方式1代表右对齐,0代表左对齐,包含宽度为0的列
164             dgp.setTitleDisplay(titleDis);
165             dgp.Print(); 
166         }
167     }
168 }

 

posted on 2012-11-29 12:05 無訫 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/casp/archive/2012/11/29/2794351.html

注意:本3.0版控件过于老旧,该控件最新版本为4.7版,您可以到http://myyouping.download.csdn.net/免费下载,也是完全免费的,没有任何功能及使用时间限制,0积分下载。我将3.0版控件的下载分提为10分,是希望大家使用最新版的控件。 控件特色: 1、超强大的DataGridView打印功能,不光可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印打印DataGridView表格,基本上能完全按DataGridView控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,如果您在使用时调整了列的顺序,刷新打印后就会按调整后的列显示顺序进行打印(这是网上很多DataGridView打印控件没有实现的),基本上做到了所见即所得的打印。 2、强大的文本打印输出功能打印文本时,如果需要,控件会自动换行或换页打印输出。 3、支持同一文档多种版面格式打印(类似于Word中的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边距),只需要在新增一页时在NewPage方法中指定要使用的页面格式即可,使用非常简单。 4、报表功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 5、打印方案保存与读取功能。可以将当前打印参数保存为打印方案文件,或都从保存的打印方案文件中读取打印参数。 6、水印功能。根据需要,可以在页面中打印或不打印以半透明空心文字打印水印。 7、特殊文字效果功能。本版控件具有打印浮雕文字、阴影文字、空心文字、块文字的功能,效果非常不错。 8、页眉页脚中既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 9、图像打印输出功能。 本控件包括两个打印组件:DGVprint打印组件与VB2008print打印组件。VB2008Print为通用打印控件(为可视化组件),可以混合打印一个或多个DataGridView表格、DataGrid表格与文字、图片等内容;而DGVprint是以VB2008Print为基础开发的(为非可视化组件),为专用的表格打印控件,可以很方便打印单个DatatGridview、DataGrid、DataTable表格的内容,并提供打印时的可视化设置界面,可设置表格打印方式(普通打印、分栏打印、跨页打印、工资条打印)、标题内容及字体、页眉页脚、选择要打印的表格列、列顺序调整及列格式设置、更改列标题名称等,使用非常方便,功能非常强大。 与本控件的上一个版本2.1版相比,本版控件新增功能如下: VB2008Print打印组件新增功能: 1、多种特效文字打印输出功能,能打印的特效文字有 空心文字、浮雕文字、块文字、阴影文字等。 2、水印打印功能。可以在面页中以半透明空心文字的形式打印背景水印,只需要简单设置控件的WaterMarkText等几个以WaterMark开头的属性即可,程序会自动打印水印。 3、PrintDGV函数可直接使用DGVPrint组件保存的打印方案文件打印输出DataGridView表格, 4、在页眉页脚中不光可以打印文本,还可以打印图像,文本与图像可同时打印(即在图像上显示文本)。此外,页眉页脚文字可以换行打印了,页面的左边也右边距也可以打印内容了(调用相应的函数PrintLeft与PrintRight实现)。 5、改进DrawText函数输出文本的功能,现在,即便调用没有指定打印区域或打印宽度的DrawText函数输出文本,打印输出时控件也会智能换行和换页(原版本是需要指定打印宽度才能自动换行换页打印的) 6、改进DrawImage与DrawCellImage输出图像功能,如果图像比较小(小于打印区域大小),可以不进行放大打印。(但如果图像大于打印区域的话,还是采用整体缩小打印,而不是区域剪裁打印)。 7、增加IsShowPrintStatusDialog属性,指示在发送到打印打印时,是否显示一个指示正在打印的状态窗口(可以取消打印),为TRUE表示要显示,为False表示不显示。 8、改进页眉页脚事件,将原来的HeaderOut与FooterOut统一为HeaderFooterOut事件,在该事件中,您可以调用PrintFooter、PrintHeader、PrintLeft、PrintRight函数分别打印上下左右的页眉。(PrintLeft与PrintRight函数为新增加的,用于在左边与右边页边距处输出内容) DGVPrint打印组件新增功能: 1、打印方案保存与读取功能。本版控件可以将您的可视化设置(包括列格式设置等)全部保存为打印方案文件(文本文件,您可以用记事本打开并修改),并有读取方案文件的功能,不再需要每次都进行打印格式设置了,一劳永逸! 2、直接调用打印方案文件打印功能。您不光可以设计DGVPrint打印组件的属性来进行打印,还可以直接调用DGVPrint组件中保存的打印方案文件,直接利用保存的方案文件的参数进行打印预览输出。 3、新增在可视化打印参数设置界面的列标题重命名功能,可能修改列标题要打印的名字。 4、水印打印功能。如果水印文本设置为空,则不打印水印。 5、导出数据成Excel功能。暂未提供该功能的函数接口,只在打印参数设置窗口中增加了一个数据导出的按钮,可以将当前要打印DataGridView的内容导出成Excel文件。该功能以后会进一步完善。 6、在进行页眉页脚文本设置时,可以用 [页码] 代表要输出的当前页码,用 [总页数] 代表要输出总页数,控件在进行输出时,会自动将其转换为对应的页码与文档总页数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值