Sie sind auf Seite 1von 4

using using using using using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.Data.OleDb; CrystalDecisions.CrystalReports.Engine; CrystalDecisions.ReportSource; CrystalDecisions.Shared; CrystalDecisions.Windows.Forms;

namespace app5 { public partial class Form1 : Form { CrystalReport1 objRpt; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { objRpt = new CrystalReport1(); string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D ataDirectory \\db1.mdb"; //Get Select query Strring and add parameters to the //Crystal report. string query = CreateSelectQueryAndParameters(); //if there is no item select,then exit from the method. if (!query.Contains("Column")) { MessageBox.Show("No selection to display!"); return; } try { OleDbConnection Conn = new OleDbConnection(connString); OleDbDataAdapter adepter = new OleDbDataAdapter(query, connStrin g); DataSet1 Ds = new DataSet1(); adepter.Fill(Ds, "Customer"); objRpt.SetDataSource(Ds); crystalReportViewer1.ReportSource = objRpt; } catch (OleDbException oleEx) { MessageBox.Show(oleEx.Message); }

catch (Exception Ex) { MessageBox.Show(Ex.Message); } } /// /// /// /// spond to /// the crystal report. /// NOTE: This parameter is used to display Coulumn names of the Crystal Report /// according to the user selecteion. /// </summary> /// <returns></returns> private string CreateSelectQueryAndParameters() { ReportDocument reportDocument; ParameterFields paramFields; ParameterField paramField; ParameterDiscreteValue paramDiscreteValue; reportDocument = new ReportDocument(); paramFields = new ParameterFields(); <summary> This method is used to 1. creat SELECT query according to the selcted column names and 2. create parameters and assign values for that parameter that corre

string query = "SELECT "; int columnNo = 0; if (chbCode.Checked) { columnNo++; query = query.Insert(query.Length, "Code as Column" + columnNo.T oString()); paramField = new ParameterField(); paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = "Customer Code"; paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } if (chbFirstName.Checked) { columnNo++; if (query.Contains("Column")) { query = query.Insert(query.Length, ", "); } query = query.Insert(query.Length, "FirstName as Column" + colum nNo.ToString()); paramField = new ParameterField();

paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = "First Name"; paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } if (chbLastName.Checked) { columnNo++; if (query.Contains("Column")) { query = query.Insert(query.Length, ", "); } query = query.Insert(query.Length, "LastName as Column" + column No.ToString()); paramField = new ParameterField(); paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = "Last Name"; paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } if (chbAddress.Checked) { columnNo++; if (query.Contains("Column")) { query = query.Insert(query.Length, ", "); } query = query.Insert(query.Length, "Address as Column" + columnN o.ToString()); paramField = new ParameterField(); paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = "Address"; paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } if (chbPhone.Checked) { columnNo++; if (query.Contains("Column")) { query = query.Insert(query.Length, ", "); } query = query.Insert(query.Length, "Phone as Column" + columnNo. ToString()); paramField = new ParameterField(); paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = "Phone";

paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } //if there is any remaining parameter, assign empty value for that //parameter. for (int i = columnNo; i < 5; i++) { columnNo++; paramField = new ParameterField(); paramField.Name = "col" + columnNo.ToString(); paramDiscreteValue = new ParameterDiscreteValue(); paramDiscreteValue.Value = ""; paramField.CurrentValues.Add(paramDiscreteValue); //Add the paramField to paramFields paramFields.Add(paramField); } crystalReportViewer1.ParameterFieldInfo = paramFields; query += " FROM Customer" ; return query; } } }

Das könnte Ihnen auch gefallen