在 阿泰的软件实用主义中看到vb版的在“水晶报表中实现任意选择指定字段显示”这篇文章
便有了把这个版本改写成web版的想法
由于以前对vb这门语言不是很熟悉,所以还是用了一些时间,设计过程请参见: http://www.cnblogs.com/babyt/articles/142309.html
公开代码如下:
//********************************************
//*
//* 此程序完成“自定义字段显示报表功能”
//*
//********************************************
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 材料采购明细" ;
SqlDataAdapter SqlAdapter = new SqlDataAdapter(SqlStr,SqlConn);
DataSet dataset = new DataSet();
SqlAdapter.Fill(dataset, "材料采购明细");
dgNameList1.DataSource = dataset.Tables["材料采购明细"].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 = "{材料采购明细." + 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;
}
}
}