找回密码
 立即注册

QQ登录

只需一步,快速开始

barrylei

中级会员

29

主题

401

帖子

900

积分

中级会员

积分
900

活字格认证

barrylei
中级会员   /  发表于:2010-10-29 16:34  /   查看:9106  /  回复:6
套打是发票类应用的一个很常见的用例,MultiRow能够实现精确的套打功能,下面我们看看它是怎么实现的。
我们以下面的发票为例:

Step:
1. 首先创建一个VS2005/2008/2010的一个WindowsForm的应用程序工程
2. 在工具箱上单击右键,然后点击【选择项】菜单,在弹出的对话框里,找到GcMultiRow,在其前面的CheckBox打勾,然后单击【确定】按钮,这样GcMulitRow就被添加到工具箱上。
3. 把GcMultiRow从工具箱拖拽到Form上,一个SmartTag会自动弹出
4. 在SmartTag里选择【添加新模板】
5. 将会出来一个欢迎界面,点击【下一步】
6. 在这个选择模板类型画面,有3种类型,在这里我们选择空白模板,关于剩下的2种模式,请参考安装包里的帮助文档】
7. 选择【空白模板】后,直接单击【完成】。
8. 会有一个关于模板的Summary的信息,单击完成按钮。
9. 在GcMultiRow上点击右键,选择【编辑模板】菜单
10. 这个时候,模板设计器就会打开,从工具箱里就可以拖拽你所需要的Cell到模板设计器上了,在【属性】窗口,你可以根据你的需求设计Cell的样式
(请参考下面的视频来设计你的模板)
http://gcdn.grapecity.com/videos/taodademo.swf
11. 最后,在你的工程里添加下面的代码,在代码里详细的描述了如何实现套打。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace Demo
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             this.gcMultiRow1.AllowUserToAddRows = false;
  20.             this.gcMultiRow1.RowCount = 5;
  21.             this.gcMultiRow1.Document = new System.Drawing.Printing.PrintDocument();
  22.             
  23.             //因为模板比较宽,所以设置Landscape为True
  24.             this.gcMultiRow1.Document.DefaultPageSettings.Landscape = true;
  25.             
  26.             //用这个事件来判断在套打的时候,哪些Cell需要打印,哪些Cell不需要打印
  27.             this.gcMultiRow1.CellFormatting += new EventHandler<GrapeCity.Win.MultiRow.CellFormattingEventArgs>(gcMultiRow1_CellFormatting);
  28.         }

  29.         void gcMultiRow1_CellFormatting(object sender, GrapeCity.Win.MultiRow.CellFormattingEventArgs e)
  30.         {
  31.             //检测Row上的Cells
  32.             if (isPrinting &amp;&amp; e.Scope== GrapeCity.Win.MultiRow.CellScope.Row)
  33.             {
  34.                 object temp = this.gcMultiRow1[e.RowIndex, e.CellName].Style.Tag;
  35.                
  36.                 //检测预先设置好的Tag标记,如果为NotPrint,把Value改为Null,这样就不打印这个Cell了。
  37.                 if (temp != null &amp;&amp; temp.ToString() == "NotPrint")
  38.                 {
  39.                     e.Value = null;
  40.                 }
  41.             }
  42.             //检测ColumnHeader区域的Cells
  43.             if (isPrinting &amp;&amp; e.Scope == GrapeCity.Win.MultiRow.CellScope.ColumnHeader)
  44.             {
  45.                 object temp= this.gcMultiRow1.ColumnHeaders[e.SectionIndex].Cells[e.CellIndex].Style.Tag;
  46.                 if (temp != null &amp;&amp; temp.ToString() == "NotPrint")
  47.                 {
  48.                     e.Value = null;
  49.                 }
  50.             }
  51.         }

  52.         private void button1_Click(object sender, EventArgs e)
  53.         {
  54.            //把发票的样子打印出来,这个时候,里面的Cells不需要填值
  55.             this.gcMultiRow1.Document.Print();

  56.         }
  57.         bool isPrinting;
  58.         private void button2_Click(object sender, EventArgs e)
  59.         {
  60.             //这个标记告诉Control,现在准备开始打印
  61.             isPrinting = true;
  62.             //设置PrintStyle为Content模式,这样我们就只打印内容,从而实现了套打功能。
  63.           this.gcMultiRow1.PrintSettings.PrintStyle= GrapeCity.Win.MultiRow.PrintStyle.Content;
  64.             this.gcMultiRow1.Document.Print();
  65.             isPrinting = false;
  66.         }
  67.     }
  68. }
复制代码
你也可以直接编译附加的工程,点击第一个Button,就可以打印发票的样子,然后输入一些值,就实现了套打功能。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

6 个回复

倒序浏览
neil
论坛元老   /  发表于:2010-11-1 09:27:00
沙发

回复 1# barrylei 的帖子

Lz  上面的取CellStyle 的方式是不是不太好?

object temp = this.gcMultiRow1[e.RowIndex, e.CellName].Style.Tag;

为什么不用GetInheritStyle 方法。
回复 使用道具 举报
barrylei
中级会员   /  发表于:2010-11-1 10:35:00
板凳
GetInheritStyle是Protect的,用不了。而且我也觉着没有必要,大多数情况下,一张发票的数据量并不是很大,我们无需太多考虑性能等问题。
回复 使用道具 举报
neil
论坛元老   /  发表于:2010-11-1 13:11:00
地板

回复 3# barrylei 的帖子

我说的是Multirow的GetInheritStyle 方法。
回复 使用道具 举报
barrylei
中级会员   /  发表于:2010-11-1 13:42:00
5#
Hi,neil,
你能贴几行代码吗?这样描述起来清楚些。:-)
回复 使用道具 举报
neil
论坛元老   /  发表于:2010-11-1 13:53:00
6#
是我记错了,multirow上没有这个方法。        
不过,
在header和footer上可以直接访问cellStyle来取tag,  在row上面可以用模板上的cell来代替。

或者不用tag, 直接记住哪些CellIndex是不需要打印的。
回复 使用道具 举报
barrylei
中级会员   /  发表于:2010-11-1 14:32:00
7#
是的,neil,你说的没错,这依赖于用户的具体实现,我这里只是给个Demo,主要是想告诉用户需要在CellFormatting里做判断。用户当然可以很灵活的写他的代码。:-)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部