本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[学习公共库]C#打印代码
麦林 发表于 2010/6/22 15:20:06

using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Drawing.Printing; using System.Data; using System.Windows.Forms; namespace WindowsApplication1 {      //// <summary>      /// Summary description for DatagridPrint.      /// </summary>      public class DatagridPrint      {          private PrintDocument printDocument;          private PageSetupDialog pageSetupDialog;          private PrintPreviewDialog printPreviewDialog;          public DatagridPrint()          {              InitializeComponent();             printDocument = new PrintDocument();              printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);          } //i是记录打印的表中的行号,lineQty是表中总共打印的行数.//count是当前页的行记数器,linesPerPage是每页可以打多少行.        int i = 0;        int lineQty = 0;        int count = 0 ;        int linesPerPage = 14;         private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)        {            int x = 30;            int y = 30;             Font font = new Font("Arial", 10);            Font PriceFont = new Font("Arial", 15, FontStyle.Bold);            Font NameFont = new Font("Arial", 11, FontStyle.Regular);            Brush brush = new SolidBrush(Color.Black);             if (!e.HasMorePages)            {                lineQty = dataGridView1.Rows.Count;                 while (count < linesPerPage && i < lineQty)                {                    if (i % 2 == 0)                    {                        e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[0].Value, NameFont, brush, x + 200, y);                         e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[1].Value, font, brush, x + 200, (y + 30));                        e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), font, brush, x + 300, (y + 30));                         e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[5].Value, PriceFont, brush, x, y + 75);                        e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[6].Value, font, brush, x + 200, y + 90);                    }                    else                    {                        e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[0].Value, NameFont, brush, x + 600, y);                         e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[1].Value, font, brush, x + 600, y + 30);                        e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), font, brush, x + 700, y + 30);                         e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[5].Value, PriceFont, brush, x + 400, y + 75);                        e.Graphics.DrawString((String)dataGridView1.Rows[i].Cells[6].Value, font, brush, x + 600, y + 90);                         y = y + 150;                    }                    count++;                    i++;                }            }             if (i < lineQty)            {                e.HasMorePages = true;            }            else            {                i = 0;                e.HasMorePages = false;            }            count = 0;        }          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);              }          }      } }

阅读全文(5078) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 

The Neurotic Fishbowl

.: 公告

fighting for the work!


Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(449)
文字感悟(103)
学习公共库(61)
生活体会(100)
资源收集(14)
.NET-Winform(27)
音乐文字(15)
心情小记(119)
SQL问题解决(10)


In the Bowl

.: 最新日志

C#打印代码
你看到的我是蓝色的
即将逝去的3月
这个假期
两个月过去
表名存在其他表时获取数据
条码打印
Remoting的用法
INI文件用法


.: 最新回复

回复:条码打印
回复:条码打印
回复:即将逝去的3月
回复:cookie对象使用
回复:条码打印
回复:JSP学习(字符串比较)
回复:应收应付核销规则及常见问题(续)
回复:两个月过去
回复:两个月过去


The Fishkeeper
blog名称:栗色?蓝色?
日志总数:449
评论数量:201
留言数量:37
访问次数:2260661
建立时间:2006年5月16日



Text Me

.: 留言板

签写新留言

链接完成
回:loseVC
来喽~
转转......
新年啦
对自己说
祝福
回:佑手
WO


Other Fish in the Sea

.: 链接


loseVC's BLOG
Collapsar_feel
布衣加针
C#and.Net
Kinogam Web




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.036 second(s), page refreshed 144806919 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号