⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 水晶报表动态添加字段(附源码及详细步骤) 环境VS.net2005 语言:C#
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;

namespace CrystalSam
{
    public partial class Form2 : Form
    {
        CrystalReport1 crReportDocument;

        public Form2()
        {
            InitializeComponent();
        }

        public Form2( string strLayerName, ArrayList arrField )
        {
            InitializeComponent();
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\Test.mdb;"; ;
            string strSql = GetQrSentence(strLayerName, arrField);
            crReportDocument = new CrystalReport1();

            OleDbConnection Conn = new OleDbConnection(connectionString);

            OleDbDataAdapter adepter = new OleDbDataAdapter(strSql, connectionString);
            DataSet1 Ds = new DataSet1();

            adepter.Fill(Ds, "Table");
            crReportDocument.SetDataSource(Ds);
            crystalReportViewer1.ReportSource = crReportDocument;
        }

        private string GetQrSentence(string strLayerName, ArrayList arrField)
        {
            ReportDocument reportDocument;
            ParameterFields paramFields;

            ParameterField paramField;
            ParameterField paramField1;
            ParameterDiscreteValue paramDiscreteValue;
            ParameterDiscreteValue paramDiscreteValue1;

            reportDocument = new ReportDocument();
            paramFields = new ParameterFields();

            string query = "SELECT ";
            int columnNo = 0;

            int arrCount = arrField.Count;
            for (int i = 0; i < arrCount; i++)
            {
                columnNo++;
                query += " " + (string)arrField[i] + " as Column" + (i + 1).ToString();
                if (i != arrCount - 1)
                {
                    query += ",";
                }
                //col1. col2..替换成数据库表内的字段名
                paramField = new ParameterField();
                paramField.Name = "col" + (i + 1).ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = (string)arrField[i];
                paramField.CurrentValues.Add(paramDiscreteValue);
                paramFields.Add(paramField);
            }

            //隐藏多余字段
            for (int j = columnNo; j < 5; j++)
            {
                paramField = new ParameterField();
                paramField.Name = "col" + (j + 1).ToString();
                paramDiscreteValue = new ParameterDiscreteValue();
                paramDiscreteValue.Value = "";
                paramField.CurrentValues.Add(paramDiscreteValue);
                paramFields.Add(paramField);
            }
            //
            crystalReportViewer1.ParameterFieldInfo = paramFields;
            
            query += " FROM " + strLayerName;
            
            return query;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -