Ejemplo n.º 1
0
        public virtual CrystalDecisions.CrystalReports.Engine.ReportDocument CreateReport()
        {
            DynamicCrystalReport rpt = new DynamicCrystalReport();

            rpt.Site = this.Site;
            return(rpt);
        }
        private string DynamicCrystalReports()
        {
            DynamicCrystalReport   oDynamicCrystalReport;
            ParameterFields        oParameterFields;
            ParameterField         oParameterField;
            ParameterDiscreteValue oParameterDiscreteValue;
            StringBuilder          oSQL = new StringBuilder("SELECT ");
            DataSet1       oDS          = new DataSet1();
            SqlConnection  oSqlConnection;
            SqlDataAdapter oSqlDataAdapter;
            SqlCommand     oSqlCommand;
            int            iColumn = 1;
            int            iColCnt = 1;

            oDynamicCrystalReport = new DynamicCrystalReport();
            oParameterFields      = new ParameterFields();

            //Assign the description text to the parameter objects for the selected
            //fields. The iColumn variable assigns the parameters in sequence based on name.
            foreach (object oItem in lstColumns.CheckedItems)
            {
                oParameterField               = new ParameterField();
                oParameterField.Name          = "DataColumn" + iColumn.ToString();
                oParameterDiscreteValue       = new ParameterDiscreteValue();
                oParameterDiscreteValue.Value = ((ListItem)oItem).Text;
                oParameterField.CurrentValues.Add(oParameterDiscreteValue);
                oParameterFields.Add(oParameterField);

                oSQL.Append(((ListItem)oItem).Value + " AS DColumn" + iColumn.ToString());
                oSQL.Append(", ");

                iColumn++;
            }

            //There are 10 hard-coded columns in this report. Place an empty string
            //in the header fields for the unused columns
            iColCnt = oDynamicCrystalReport.ParameterFields.Count;

            for (int i = iColumn; i <= iColCnt; i++)
            {
                oParameterField               = new ParameterField();
                oParameterField.Name          = "DataColumn" + iColumn.ToString();
                oParameterDiscreteValue       = new ParameterDiscreteValue();
                oParameterDiscreteValue.Value = string.Empty;
                oParameterField.CurrentValues.Add(oParameterDiscreteValue);
                oParameterFields.Add(oParameterField);

                iColumn++;
            }

            //Pass in the collection of 10 named parameter objects
            crystalReportViewer1.ParameterFieldInfo = oParameterFields;

            //Whack the trailing comma
            oSQL.Remove(oSQL.Length - 2, 2);

            oSQL.Append(" FROM Employees");

            //Execute the SQL
            oSqlConnection = new SqlConnection("Data Source=SETON-NOTEBOOK;Initial catalog=Northwind;" +
                                               "Integrated security=SSPI;Persist security info=False");
            oSqlConnection.Open();

            oSqlDataAdapter = new SqlDataAdapter();
            oSqlCommand     = new SqlCommand();

            oSqlCommand.CommandText       = oSQL.ToString();
            oSqlCommand.Connection        = oSqlConnection;
            oSqlDataAdapter.SelectCommand = oSqlCommand;

            //Make sure the table name matches the one used in DataSet1.Designer.cs.
            //DataTable1 is the default name. If you fail to name it properly, the
            //headers will appear but the report data will not.
            oSqlDataAdapter.Fill(oDS, "DataTable1");

            //Assign the DataTable to the report
            oDynamicCrystalReport.SetDataSource(oDS);
            crystalReportViewer1.ReportSource = oDynamicCrystalReport;

            return(oSQL.ToString());
        }