指定数据字段生成报表(C#+Crystal Report 10)
生成效果图:/Files/jerrie/pic.rar
代码:/Files/jerrie/CRDynamicField.rar
绑定数据集
动态指定字段名
重新绑定参数,数据集
代码:/Files/jerrie/CRDynamicField.rar
绑定数据集
1private void SetBind()
2 {
3 SqlConnection sqlcon=new SqlConnection ("server=localhost;uid=sa;pwd=pass;database=Northwind");
4 SqlDataAdapter sqlda=new SqlDataAdapter ("select top 20* from customers",sqlcon);
5 DataSet ds=new DataSet ();
6 sqlda.Fill (ds);
7 cydy.SetDataSource (ds);
8 CrystalReportViewer1.ReportSource =cydy;
9 }
2 {
3 SqlConnection sqlcon=new SqlConnection ("server=localhost;uid=sa;pwd=pass;database=Northwind");
4 SqlDataAdapter sqlda=new SqlDataAdapter ("select top 20* from customers",sqlcon);
5 DataSet ds=new DataSet ();
6 sqlda.Fill (ds);
7 cydy.SetDataSource (ds);
8 CrystalReportViewer1.ReportSource =cydy;
9 }
动态指定字段名
int count=0;
if (CheckBox1.Checked ) checkhash.Add ("CompanyName","公司名称");//checklist.Add ("CompanyName");
if (CheckBox2.Checked ) checkhash.Add ("Address","地址");// checklist.Add ("Address");
if (Checkbox3.Checked ) checkhash.Add ("City","城市");// checklist.Add ("City");
if (Checkbox4.Checked ) checkhash.Add ("Country","国家");// checklist.Add ("Country");
if (Checkbox5.Checked ) checkhash.Add ("Fax","传真");// checklist.Add ("Fax");
//赋值给公式和参数
foreach(DictionaryEntry o in checkhash)
{
paramfield=new ParameterField ();
paramfield.ParameterFieldName="paramdyfield"+(count+1).ToString();
paramdiscrete=new ParameterDiscreteValue ();
paramdiscrete.Value =o.Value .ToString ();//checklist[i].ToString ();
paramfield.CurrentValues .Add (paramdiscrete);
paramfield.AllowCustomValues =false;
paramfields.Add (paramfield);
cydy.DataDefinition .FormulaFields ["dyfield"+(count+1).ToString()].Text ="{Customers."+o.Key .ToString ()+"}";
count++;
}
不允许其它报表参数弹出提示 if (CheckBox1.Checked ) checkhash.Add ("CompanyName","公司名称");//checklist.Add ("CompanyName");
if (CheckBox2.Checked ) checkhash.Add ("Address","地址");// checklist.Add ("Address");
if (Checkbox3.Checked ) checkhash.Add ("City","城市");// checklist.Add ("City");
if (Checkbox4.Checked ) checkhash.Add ("Country","国家");// checklist.Add ("Country");
if (Checkbox5.Checked ) checkhash.Add ("Fax","传真");// checklist.Add ("Fax");
//赋值给公式和参数
foreach(DictionaryEntry o in checkhash)
{
paramfield=new ParameterField ();
paramfield.ParameterFieldName="paramdyfield"+(count+1).ToString();
paramdiscrete=new ParameterDiscreteValue ();
paramdiscrete.Value =o.Value .ToString ();//checklist[i].ToString ();
paramfield.CurrentValues .Add (paramdiscrete);
paramfield.AllowCustomValues =false;
paramfields.Add (paramfield);
cydy.DataDefinition .FormulaFields ["dyfield"+(count+1).ToString()].Text ="{Customers."+o.Key .ToString ()+"}";
count++;
}
1for(int j=count+1;j<6 ;j++)
2 {
3 paramfield=new ParameterField ();
4 paramfield.ParameterFieldName="paramdyfield"+j.ToString();
5 paramdiscrete=new ParameterDiscreteValue ();
6 paramdiscrete.Value ="";
7 paramfield.CurrentValues .Add (paramdiscrete);
8 paramfield.AllowCustomValues =false;
9 paramfields.Add (paramfield);
10 }
2 {
3 paramfield=new ParameterField ();
4 paramfield.ParameterFieldName="paramdyfield"+j.ToString();
5 paramdiscrete=new ParameterDiscreteValue ();
6 paramdiscrete.Value ="";
7 paramfield.CurrentValues .Add (paramdiscrete);
8 paramfield.AllowCustomValues =false;
9 paramfields.Add (paramfield);
10 }
重新绑定参数,数据集
1CrystalReportViewer1.ParameterFieldInfo =paramfields;
2 //重新绑定数据集
3 SetBind();
2 //重新绑定数据集
3 SetBind();
在WIN2003+NET 2003+SQL 2000下编绎通过。