自定义字段显示报表功能

公开代码如下:
//********************************************
//*
//*  此程序完成“自定义字段显示报表功能”
//*
//********************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports .Engine;
using System.Data.SqlClient;
namespace Crystal_Report
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.CheckBox CheckBox1;
  protected System.Web.UI.WebControls.CheckBox CheckBox2;
  protected System.Web.UI.WebControls.CheckBox CheckBox3;
  protected System.Web.UI.WebControls.CheckBox CheckBox4;
  protected System.Web.UI.WebControls.CheckBox CheckBox5;
  protected System.Web.UI.WebControls.CheckBox CheckBox6;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DataGrid dgNameList1;
  protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   Label1.Text="";


  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   string ConnectionStr="";
   string SqlStr="";
   string FieldStr="";
   string [] FieldArray;
   ParameterFields ParamFields = new ParameterFields();
   //ParameterField ParamField = new ParameterField();
   //ParameterDiscreteValue DiscreteVal = new ParameterDiscreteValue();
   int i,j;

   if(CheckBox1.Checked == true)
    FieldStr=CheckBox1.Text;
   if(CheckBox2.Checked == true)
    FieldStr=FieldStr+","+CheckBox2.Text;
   if(CheckBox3.Checked == true)
    FieldStr=FieldStr+","+CheckBox3.Text;
   if(CheckBox4.Checked == true)
    FieldStr=FieldStr+","+CheckBox4.Text;
   if(CheckBox5.Checked == true)
    FieldStr=FieldStr+","+CheckBox5.Text;
   if(CheckBox6.Checked == true)
    FieldStr=FieldStr+","+CheckBox6.Text;
   if(FieldStr=="")
   {
    Label1.Text="请选择要显示的字段";
    return;
   }

   if (FieldStr.Substring(0,1)==",")
    FieldStr = FieldStr.Substring(1, FieldStr.Length - 1);
   FieldArray = FieldStr.Split(',');

   ConnectionStr="data source=localhost;uid=sa;pwd=12345;database=Pubs";
   SqlConnection SqlConn = new SqlConnection(ConnectionStr);
   SqlStr="Select " + FieldStr + " From orders" ;
   SqlDataAdapter SqlAdapter = new SqlDataAdapter(SqlStr,SqlConn);
   
   DataSet dataset = new DataSet();
   SqlAdapter.Fill(dataset, "orders");
   dgNameList1.DataSource = dataset.Tables["orders"].DefaultView;
   dgNameList1.DataBind();
   
   CrystalReport crReportDocument = new CrystalReport();
   for(i=0;i<FieldArray.Length;i++)
   {
    ParameterField ParamField = new ParameterField();
    ParamField.ParameterFieldName = "myParaField"+Convert.ToString(i+1);//设置要修改的参数的名称
    ParameterDiscreteValue DiscreteVal = new ParameterDiscreteValue();
    DiscreteVal.Value=FieldArray[i];
    ParamField.CurrentValues.Add(DiscreteVal);
    ParamFields.Add(ParamField);
    //ParamField.AllowCustomValues = false;
    string MyStr="myField"+Convert.ToString(i+1);
    crReportDocument.DataDefinition.FormulaFields[MyStr].Text = "{orders." + FieldArray[i] + "}";
    //string temp = crReportDocument.DataDefinition.FormulaFields[MyStr].Text;
    
  
    }
   for(j=i+1;j<=6;j++)
   {
    ParameterField ParamField = new ParameterField();
    ParamField.ParameterFieldName = "myParaField" + Convert.ToString(j);
    ParamFields.Add(ParamField);
    ParameterDiscreteValue DiscreteVal = new ParameterDiscreteValue();
    DiscreteVal.Value = "";
    ParamField.CurrentValues.Add(DiscreteVal);
    ParamFields.Add(ParamField);
    //ParamField.AllowCustomValues = False;

   }
   CrystalReportViewer1.ParameterFieldInfo = ParamFields;
   crReportDocument.SetDataSource(dataset);
   CrystalReportViewer1.ReportSource = crReportDocument;

  }
 }
}

posted @ 2006-10-20 16:22  '.Elvis.'  阅读(344)  评论(0编辑  收藏  举报