如何在C#中实现DATAGRIDVIEW 数据打印

本文详细介绍了如何在C#中实现DATAGRIDVIEW的数据打印,包括基本操作、打印事件初始化、事件响应方法以及调用打印和预览打印的步骤。示例代码展示了如何使用Graphics对象绘制数据,并提供了打印预览功能。
摘要由CSDN通过智能技术生成

如何在C#中实现DATAGRIDVIEW 数据打印

分享下关于 C#中 实现打印功能的代码   希望对初学者有所帮助

关于打印格式感觉很痛苦 我一般选择两种方式 一种是通过 DRAWING 格式,另外一种通过HTML代码来控制,都特别麻烦,不知道各位高手能否提供一些更好的建议?

第一步 基本操作

定义 一个C#提供的 PrintDocument 对象

private PrintDocument printDocument;

第二步

//写一个方法 对打印事件进行初始化

下载: Init.cs
  1. private void PrintDocument ()
  2. {
  3. printDocument = new   PrintDocument () ;
  4. printDocument . PrintPage += new   PrintPageEventHandler ( printDocument_PrintPage ) ;
  5. }

第三步

事件响应方法

下载: Action.cs
  1. private void printDocument_PrintPage ( object sender , PrintPageEventArgs e )
  2. {
  3. //StringReader lineReader = new StringReader(textBox.Text);
  4. Graphics   graphic = e . Graphics ; //获取绘图对象
  5. float linesPerPage = 0 ; //页面行号
  6. float yPosition = 0 ; //绘制字符串的纵向位置
  7. float leftMargin = e . MarginBounds . Left ; //左边距
  8. float topMargin = e . MarginBounds . Top ; //上边距
  9. string   line = string . Empty ; //读取的行字符串
  10. int currentPageLine = 0 ; //当前页读取的行数
  11. Font   charFont = button1 . Font ; //获取打印字体
  12. SolidBrush   brush = new SolidBrush ( Color . Black ) ; //刷子
  13. linesPerPage = e . MarginBounds . Height / charFont . GetHeight ( graphic ) ; //每页可打印的行数
  14. //countNum记录全局行数,currentPageLine记录当前打印页行数。
  15. // graphic.DrawString("水费通知单", charFont, brush, 360, 100, new StringFormat());
  16.  
  17. //房产地址textbox3
  18. //租户名字textbox2
  19. // 上次缴费记录 textbox5
  20. //费用到期 textbox7
  21. //合同开始时间 textbox4
  22. //合同到期时间 textbox6
  23. //月租金 textbox8
  24. //月管理费 textbox9
  25. //缴费租金 textbox11
  26. //缴费管理费 textbox10
  27. //总计   textbox12
  28.  
  29. graphic . DrawString ( " 武大教育发展有限公司房屋租金缴费登记 " , charFont , brush , 300 , 100 , new   StringFormat ()) ;
  30. graphic . DrawString ( " 租户:         " + this . textBox2 . Text , charFont , brush , 100 , 140 , new   StringFormat ()) ;
  31. graphic . DrawString ( " 合同号:       " + this . textBox1 . Text , charFont , brush , 100 , 180 , new   StringFormat ()) ;
  32. graphic . DrawString ( " 房产地址:     " + this . textBox3 . Text , charFont , brush , 100 , 220 , new   StringFormat ()) ;
  33. graphic . DrawString ( " 月租金:       " + this . textBox8 . Text , charFont , brush , 100 , 260 , new   StringFormat ()) ;
  34. graphic . DrawString ( " 月管理费:     " + this . textBox9 . Text , charFont , brush , 100 , 300 , new   StringFormat ()) ;
  35. graphic . DrawString ( " 上次缴费至:   " + this . textBox7 . Text , charFont , brush , 100 , 340 , new   StringFormat ()) ;
  36. graphic . DrawString ( " 本次缴费至:   " + this . dateTimePicker1 . Text , charFont , brush , 100 , 370 , new   StringFormat ()) ;
  37. graphic . DrawString ( " 租金:         " + this . textBox11 . Text , charFont , brush , 100 , 410 , new   StringFormat ()) ;
  38. graphic . DrawString ( " 管理费:       " + this . textBox10 . Text , charFont , brush , 100 , 450 , new   StringFormat ()) ;
  39. graphic . DrawString ( " 合计:         " + this . textBox12 . Text , charFont , brush , 100
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值