zoukankan      html  css  js  c++  java
  • 使用参数动态显示要打印的数据

    详细步骤:
    step1:创建数据集:右击项目/添加/新增项/选择数据集/取名ds_pubs/点击"添加"按钮
    step2:在服务器资源管理器中点击"连接到数据库",连接到pubs数据库,并将其中的titles表拖到数据集ds_pubs中
    step3:创建水晶报表:右击项目/添加/新增项/选择crystalReport报表/取名rpt_pubs.rpt/点击"添加"按钮
    step4:设计报表:在字段资源管理器中,右击"数据库字段"/展开"项目数据"/展开"ADO.NET数据集"/选中ds_pubs将其添加到右边窗口里/确定
    step5:在字段资源管理器中,右击"公式字段",添加10个公式字段:myField1,myField2.....myField10
    step6:在字段资源管理器只,右击"参数字段",添加10个参数字段:myParaField1,myParaField2......myParaField10


    step7:报表布局:

    step8:创建窗体:frm_pubs


    step9:定义Click事件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;

    namespace teast
    {
        
    public partial class frm_test : Form
        
    {
            
    public frm_test()
            
    {
                InitializeComponent();
            }


            SqlConnection conn 
    = new SqlConnection("server=zzy;integrated security=sspi;database=pubs");
            SqlDataAdapter da;
            DataSet ds 
    = new DataSet();

            
    private void button1_Click(object sender, EventArgs e)
            
    {
                
    string strField = ""//保存SQL语句中要查询的字段,如:字段1,字段2
                string[] strFieldArray;
                
    try
                
    {
                    
    //设置SQL查询语句
                    if (checkBox1.Checked == true)
                        strField 
    = checkBox1.Text;
                    
    if (checkBox2.Checked == true)
                        strField 
    = strField + "," + checkBox2.Text;
                    
    if (checkBox3.Checked == true)
                        strField 
    = strField + "," + checkBox3.Text;
                    
    if (checkBox4.Checked == true)
                        strField 
    = strField + "," + checkBox4.Text;
                    
    if (checkBox5.Checked == true)
                        strField 
    = strField + "," + checkBox5.Text;
                    
    if (checkBox6.Checked == true)
                        strField 
    = strField + "," + checkBox6.Text;
                    
    if (checkBox7.Checked == true)
                        strField 
    = strField + "," + checkBox7.Text;
                    
    if (checkBox8.Checked == true)
                        strField 
    = strField + "," + checkBox8.Text;
                    
    if (checkBox9.Checked == true)
                        strField 
    = strField + "," + checkBox9.Text;
                    
    if (checkBox10.Checked == true)
                        strField 
    = strField + "," + checkBox10.Text;
                    
    if (strField == "")
                    
    {
                        MessageBox.Show(
    "请选择要显示的字段""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
    return;
                    }

                    
    //if (strField.Substring(0, 1) == ",")
                    
    //strField = strField.Substring(1, strField.Length - 1);
                    strFieldArray = strField.Split(','); 

                    
    string strsql = "select " + strField + " from titles";
                    da 
    = new SqlDataAdapter(strsql, conn);
                    da.Fill(ds, 
    "titles");

                    
    //设置参数
                    ParameterFields paramFields = null;  //参数字段集合
                    ParameterField paramField;   //参数字段 
                    ParameterDiscreteValue discreteVal;  //离散值

                    
    //
                    rpt_test rpt = new rpt_test();
                    
    for (int i = 0; i < strFieldArray.Length; i++)
                    
    {
                        
    //设置参数部分
                        paramField = new ParameterField();
                        paramField.Name 
    = "myParaField" + (i + 1).ToString();

                        
    //设置离散值
                        discreteVal = new ParameterDiscreteValue();
                        discreteVal.Value 
    = strFieldArray[i];

                        
    //把离散值赋值给参数字段
                        paramField.CurrentValues.Add(discreteVal);

                        
    //把字段添加到字段集合中
                        paramFields = new ParameterFields();
                        paramFields.Add(paramField);

                        
    //不允许该字段弹出显示
                        paramField.AllowCustomValues = false;

                        
    //将公式和字段值绑定在一起
                        rpt.DataDefinition.FormulaFields["myField" + (i + 1).ToString()].Text = "{titles." + strFieldArray[i] + "}";
                    }

                    
    for (int j = 0; j < 10; j++)
                    
    {
                        paramField 
    = new ParameterField();
                        paramField.Name 
    = "myParaField" + j.ToString();
                        paramFields.Add(paramField);
                        discreteVal 
    = new ParameterDiscreteValue();
                        discreteVal.Value 
    = "";
                        paramField.CurrentValues.Add(discreteVal);
                        paramFields.Add(paramField);
                        paramField.AllowCustomValues 
    = false;
                    }

                    rpt.SetDataSource(
    this.ds.Tables["titles"]);
                    crystalReportViewer1.ReportSource 
    = rpt;
                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(ex.ToString());
                }

            }

        }

    }
    改自于:阿泰的软件实用主义
  • 相关阅读:
    SQL Server 备份方案
    Azure 学习笔记
    SEO – 大杂烩
    Asp.net core 学习笔记之 Tag Helper
    读取注册表
    DOM学习历程-3
    inno setup给控制的那边加图标
    C++生成exe安装到别人那边无法使用缺少dll
    inno setup 最后
    inno setup
  • 原文地址:https://www.cnblogs.com/perfect/p/582835.html
Copyright © 2011-2022 走看看