动态水晶报表:任意表,任意列 之 动态格线实现

在前文《动态水晶报表:任意表,任意列,以及动态格线》中,
有一个地方还没有完全实现。就是根据内容自动调整表头的列,跟内容表格相对应。

我们也注意到了,在前文中,后面的列之所以不显示,不是因为它们没有了,它们是存在的,只是内容是空(''),注意是空,不是空格。
依此延伸开来,表头也可以用此原理来实现。

本文接上文,稍加改动
把原来的表头删除,放上6个参数字段,p1,p2...p6,都设置边框。设置好与字段的对齐。

然后,改进一下窗体的代码即可。

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.ComponentModel;
 4  using  System.Data;
 5  using  System.Text;
 6  using  System.Windows.Forms;
 7  using  CrystalDecisions.Shared;
 8  using  CrystalDecisions.CrystalReports.Engine;
 9  using  CrystalDecisions.Windows.Forms;
10  using  System.Data.OleDb;
11  namespace  DyCrystalReportDemo
12  {
13       public   partial   class  Form1 : Form
14      {
15           public  Form1()
16          {
17              InitializeComponent();
18          }
19 
20           private   void  button1_Click( object  sender, EventArgs e)
21          {
22              String tblName  =  comboBox1.Text ;
23               String connstr  =   " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "   +  System.Threading.Thread.GetDomain().BaseDirectory +   " bbtcrall.mdb "   +   " ; " ;
24              
25               if  (tblName  ==   "" )
26              {
27                  MessageBox.Show( " 请选择表名 " );
28                  comboBox1.Focus();
29                   return ;
30              }
31 
32               // 打开数据库连接
33              
34              DataTable dt1  =   new  DataTable();
35              DataTable dtx  =   new  DataTable();
36              OleDbDataAdapter da  =   new  OleDbDataAdapter();
37              OleDbConnection cn  =   new  OleDbConnection(connstr);
38              
39               // 打开选择的表(注意进行错误保护)
40 
41               // 如果要实现任意列,只要更改此处的SQL为具体的字段即可
42              da  =   new  OleDbDataAdapter( " SELECT * From  "   +  tblName, cn);
43              da.Fill(dt1);
44 
45               // 根据dtl获得实际列数
46               int  cols  =  dt1.Columns.Count;
47              
48 
49               // 处理ds1
50              clsDyCrystalReportCore xCore  =   new  clsDyCrystalReportCore();
51              dtx  =  xCore.dtx(dt1);
52              
53              ReportDocument myReport  =   new  ReportDocument();
54               string  reportPath  =  System.Threading.Thread.GetDomain().BaseDirectory  +   " crystalreport1.rpt " ;
55              myReport.Load(reportPath);
56 
57               // 绑定数据集,注意,一个报表用一个数据集。
58 
59              myReport.SetDataSource(dtx);
60                  
61               // 设置参数,即表头
62               for  ( int  i  =   1 ; i  <=   6 ; i ++
63              {
64                   if  (i  <=  cols)
65                      myReport.SetParameterValue( " p "   +  i.ToString(),  " 列名 "   +  i.ToString());
66                   else
67                       // 注意,这个不能省,一定要给没用到的参数一个空值
68                      myReport.SetParameterValue( " p "   +  i.ToString(),  "" );          
69              }
70              crystalReportViewer1.ReportSource  =  myReport;
71               // crystalReportViewer1.RefreshReport();
72          }
73 
74           private   void  Form1_Load( object  sender, EventArgs e)
75          {          
76              comboBox1.Items.Add ( " Test1_1 " );
77              comboBox1.Items.Add ( " Test1_2 " );        
78          }
79 
80           private   void  crystalReportViewer1_Load( object  sender, EventArgs e)
81          {
82 
83          }
84      }
85  }
86 

不好意思,偷懒了,没对齐。

稍加说明:

1: //根据dtl获得实际列数
      int cols = dt1.Columns.Count;

2: //设置参数,即表头
            for (int i = 1; i <= 6; i++)
            {
                if (i <= cols)
                    myReport.SetParameterValue("p" + i.ToString(), "列名" + i.ToString());
                else
                    //注意,这个不能省,一定要给没用到的参数一个空值
                    myReport.SetParameterValue("p" + i.ToString(), "");         
            }

3://crystalReportViewer1.RefreshReport();

这个要注意,这里注释掉了,因为每次刷新的时候,参数窗口会重新弹出来。
所以要使用参数功能的话,尽量把toolbar上的刷新按钮禁止掉。防止出问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值