CSDN博客

img bccu

在水晶报表中动态的添加自己所需要的的字段的代码

发表于2004/9/18 16:38:00  866人阅读

分类: 学习笔记

         在水晶报表中动态的添加自己所需要的的字段的代码

//首先实例化一个报表(zkck)cr1
      reports.zkck cr1=new gjmis.wzsys.reports.zkck ();
   conn=new SqlConnection (this.strcon );
   sqldap=new SqlDataAdapter ();
   selcmd=new SqlCommand();
   sqldap.SelectCommand=selcmd;
   selcmd.CommandText ="[zkckbd]";
   //调用存储过程
   selcmd.CommandType=CommandType.StoredProcedure;
   selcmd.Connection =conn;
   //接收通过网页传递过来的参数(即要在报表上要显示的字段)
   string []field=Regex.Unescape((Request["strcheck"])).Split(',');
   selcmd.Parameters.Add("@imonth",Convert.ToInt32 (Request["imonth"]));
   sqldap.Fill (sqldst,"zkck");
      //实例化报表中的文本对象
   TextObject[] txt=new TextObject[17];
           TextObject[] txt_1=new TextObject [17];
   TextObject[] txt_2=new TextObject [17];
   //实例化报表中的自定义公式
   FormulaFieldDefinition[] field_1=new FormulaFieldDefinition[17];
   FormulaFieldDefinition[] field_2=new FormulaFieldDefinition[17];
   //实例化报表中的自定义公式(对详细资料中的字段进行汇总)
   FormulaFieldDefinition[] tot_1=new FormulaFieldDefinition[18];
   FormulaFieldDefinition[] tot_2=new FormulaFieldDefinition[18];
   for(int i=1;i<=17;i++)
   {
     //为实例化的报表文本与报表中的文本对象相对应
    txt[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt"+Convert.ToString (i)];
    txt_1[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt_1"+Convert.ToString (i)];
    txt_2[i-1]=(TextObject)cr1.ReportDefinition.ReportObjects["txt_2"+Convert.ToString (i)];
    //为实例化的自定义公式与报表中的自定义公式相对应
    field_1[i-1]=cr1.DataDefinition.FormulaFields["field_1"+Convert.ToString (i)];
    field_2[i-1]=cr1.DataDefinition.FormulaFields["field_2"+Convert.ToString (i)];
    tot_1[i-1]=cr1.DataDefinition.FormulaFields["tot_1"+Convert.ToString (i)];
    tot_2[i-1]=cr1.DataDefinition.FormulaFields["tot_2"+Convert.ToString (i)];
    //初始设置所有报表中的所有对象为不显示
    cr1.ReportDefinition.ReportObjects["tot1"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
           cr1.ReportDefinition.ReportObjects["tot2"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
    cr1.ReportDefinition.ReportObjects["field1"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
    cr1.ReportDefinition.ReportObjects["field2"+Convert.ToString (i)+"1"].ObjectFormat .EnableSuppress = true;
    cr1.ReportDefinition.ReportObjects["ln1"+Convert.ToString(i)].ObjectFormat .EnableSuppress =true;
    cr1.ReportDefinition.ReportObjects["ln2"+Convert.ToString(i)].ObjectFormat .EnableSuppress =true;
   }
   for(int i=0;i<field.Length-1;i++)
   {
     //为要显示的自定义的文本对象赋值(即同时给报表中的文本对象赋值)
    txt[i].Text=field[i].ToString();
    txt_1[i].Text="张数";
    txt_2[i].Text="金额";
    //为要显示的自定义的公式赋值(即同时给报表中的自定义公式赋值)
    field_1[i].Text="{zkckbd."+field[i] +"张数}";
    tot_1[i].Text="sum({zkckbd."+field[i] +"张数})";
              field_2[i].Text="{zkckbd."+field[i] +"}";
    tot_2[i].Text="sum({zkckbd."+field[i] +"})";
        //赋值过的对象(要在报表中显示的字段)让它们显示
    cr1.ReportDefinition.ReportObjects["field1"+Convert.ToString(i+1)+"1"].ObjectFormat.EnableSuppress=false;
    cr1.ReportDefinition.ReportObjects["field2"+Convert.ToString(i+1)+"1"].ObjectFormat.EnableSuppress=false;
    cr1.ReportDefinition.ReportObjects["tot1"+Convert.ToString (i+1)+"1"].ObjectFormat .EnableSuppress = false;
    cr1.ReportDefinition.ReportObjects["tot2"+Convert.ToString (i+1)+"1"].ObjectFormat .EnableSuppress = false;
    cr1.ReportDefinition.ReportObjects["ln1"+Convert.ToString(i+1)].ObjectFormat.EnableSuppress =false;
    cr1.ReportDefinition.ReportObjects["ln2"+Convert.ToString(i+1)].ObjectFormat.EnableSuppress =false;
   
   }
   tot_1[17]=cr1.DataDefinition.FormulaFields["tot_118"];
   tot_2[17]=cr1.DataDefinition.FormulaFields["tot_218"];
   //
           tot_1[17].Text ="sum({zkckbd.张数})";
   tot_2[17].Text ="sum({zkckbd.合计})";
   //对一些始终显示的文本的位置进行定位
//   cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot1181"].ObjectFormat.EnableSuppress =false;
//   cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot2181"].ObjectFormat.EnableSuppress =false;
   cr1.ReportDefinition.ReportObjects["tot1181"].Left=cr1.ReportDefinition.ReportObjects["tot1181"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["tot2181"].Left=cr1.ReportDefinition.ReportObjects["tot2181"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["fieldzs1"].Left=cr1.ReportDefinition.ReportObjects["fieldzs1"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["fieldje1"].Left=cr1.ReportDefinition.ReportObjects["fieldje1"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["txt_hj"].Left=cr1.ReportDefinition.ReportObjects["txt_hj"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["txt_je"].Left=cr1.ReportDefinition.ReportObjects["txt_je"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["txt_zs"].Left=cr1.ReportDefinition.ReportObjects["txt_zs"].Left-(18-field.Length)*1590;
   cr1.ReportDefinition.ReportObjects["txtzbr"].Left=cr1.ReportDefinition.ReportObjects["txt_zs"].Left ;                                           cr1.ReportDefinition.ReportObjects["txt_title"].Width=1845+cr1.ReportDefinition.ReportObjects["txt1"].Width*(field.Length);
   //文本及字段的移动成功(下面是对一些线作出必要的调整) 
   lbltmp.Text="<script>var bl;var kd; var zs;var je;var t_kd; bl=parseInt(ln0.style.left);kd=1590*bl/1845;t_kd=1740*bl/1845;zs=470*bl/1845; je=1120*bl/1845; ln_hj1.style.left=bl+zs+"+(field.Length-1)+"*kd;ln_hj2.style.left=bl+"+(field.Length)+"*kd;lntop1.style.width=t_kd+"+(field.Length)+"*kd;lntop2.style.width="+(field.Length)+"*kd;lntop3.style.width=t_kd+"+(field.Length)+"*kd;lntop5.style.width=t_kd+"+(field.Length)+"*kd;for(var i=0;i<"+sqldst.Tables[0].Rows.Count+";i++)lntop4[i].style.width=t_kd+"+(field.Length)+"*kd;</script>";

   cr1.SetDataSource(this.sqldst.Tables["zkck"]);
   CrystalReportViewer1.ReportSource =cr1;
  }

0 0

相关博文

我的热门文章

img
取 消
img