cancel
Showing results for 
Search instead for 
Did you mean: 

C# - update Crystal Report in runtime

Former Member
0 Kudos

I try to solve problem, but I am still without success.

I work on Win 8.1, VS2013 community.

I have simple Winform application with DatagridView and Button. Datagridview has one column and is filled with sample values in constructor of form.

            private List<StringValue> list;

        private List<Report> lr;

        public Form1()

        {

            InitializeComponent();

            list = new List<StringValue>();

            lr = new List<Report>();

            list.Add(new StringValue("bla1"));

            list.Add(new StringValue("bla2"));

            dataGridView1.DataSource = list;

        }

As a part of the project I have also dataset component, which is filled after button click. and for each row of datagridview is created crystal report and its name and added to the list.

            private void button1_Click(object sender, EventArgs e)

        {

            int i = 1;

            ReportDocument cr;

            try

            {

            foreach (DataGridViewRow row in dataGridView1.Rows)

            {

               

                cr = new CrystalReport1();

                dataSet11.DataTable1.Rows.Clear();

                cr.SetDataSource(dataSet11);

                DataRow nr = dataSet11.DataTable1.NewRow();

                nr["sloup1"] = row.Cells["sloup1"].Value;

                dataSet11.DataTable1.Rows.Add(nr);

                cr = new CrystalReport1();

                cr.SetDataSource(dataSet11);

                cr.Refresh();

                lr.Add(new Report(i.ToString(), cr));

                i++;

            }

                ReportViewer viewer = new ReportViewer(lr);

                viewer.Show();

            }

            catch (Exception ex)

            { }

        }

    }

Report class is

    public class Report

    {

        private  string m_name = null;

        private CrystalDecisions.CrystalReports.Engine.ReportDocument m_document;

        public string Name

        {

            get{return m_name;}

            set{m_name = value;}

        }

        public ReportDocument reportDocument

        {

            get { return m_document; }

            set { m_document = value; }

        }

        public Report(string name, ReportDocument doc)

        {

            m_name = name;

            m_document = doc;

        }

    }

I created also other form with listBox, where are displayed names of reports and crystalreportviewer control. After click on the item in listbox, I want to open crystalreport sent from form1. Here is source code:

    List<Report> lr = null;

        public ReportViewer()

        {

            InitializeComponent();

        }

        public ReportViewer(List<Report> reports) : this()

        {

            lr = reports;

            foreach (Report report in reports)

            {

                listBox1.Items.Add(report.Name);

            }

            listBox1.SelectedIndex = 0;

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            crView.ReportSource = null;

            ReportDocument rep = new ReportDocument();

            rep = GetReport(listBox1.SelectedIndex);

            crView.ReportSource = rep;

            this.crView.RefreshReport();

           

        }

        private ReportDocument GetReport(int p)

        {

            return lr[p].reportDocument;

        }

    }

and now what is problem: when I click on the item in listbox, in viewer is displayed only last report. There are two items with different name, but onlz one report.

If i tried show reports for each created report at form1, values and reports were OK, but in the selectedindexchanged event both reportdocument are the same.

Where is error please?

How to update crzstal report, so it will display correct data?

thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

DellSC
Active Contributor
0 Kudos

Because of the overhead for running a Crystal report, I would NOT do this on change of the selection in the listbox.  For example, if a user were to use the up or down arrow keys to move to a report in the listbox, each report would get loaded - NOT a good user experience.  Instead, I would use a separate button or a double-click on the item to open the report.

Beyond that, this is an issue with the information coming from the listbox properties - not with Crystal.  So, I would do some old-fashioned debugging, without trying to open any report, and see what values are being returned for the SelectedIndex in the listbox.

-Dell

Answers (0)