在水晶報表中動態的添加自己所需要的的字段的代碼

         在水晶報表中動態的添加自己所需要的的字段的代碼

//首先實例化一個報表(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;
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章