none
C# example: 2 reports with one report control RRS feed

  • Question

  • I have about 8 hours experience with the report control, most of which was looking for a clue on how to get two reports to work with one reportviewer control.  So I decided to look at the form.designer.cs code to see how Visual Studios was doing it.  I copied the pertinent section of the Initialize Components section (first block of code) into a button click event. I saw that the initial report had a tableAdapter and a bindingSource associated with it.  I added a new adapter and binding source to the project and bound them to the second report in similar fashion in the way VS had bound the adapter and bindingSource to the initial report.  I then tweaked the code in the button click event block to point to the new adapter and binding source that I added.  It works like a charm.  I haven't looked at what could and couldn't be deleted and still work.  There may be a simpler more elegant solution, but I couldn't find one.  The project has 1 dataSet, two tableAdapters & two bindingSources.  I hope you find this helpful.

    fastboat

    namespace ReportsApplication
    
    
    
    {
    
        public partial class Form1 : Form
    
    
        {
    
    
            public Form1()
    
    
            {
    
    
               InitializeComponent();
    
    
            }
    
    
    
    
    
    
    
            private void Form1_Load(object sender, EventArgs e)
    
    
    
            {
    
    
    
                // TODO: This line of code loads data into the 'Auto_1DataSet.Fuel' table. You can move, or remove it, as needed.
    
    
    
                this.FuelTableAdapter.Fill(this.Auto_1DataSet.Fuel);
    
    
    
                this.reportViewer1.RefreshReport();
    
    
    
            }
    
    
    
    
    
    
    
            private void button1_Click(object sender, EventArgs e)
    
    
    
            {
    
    
    
                reportViewer1.Reset();
    
    
    
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
    
    
                reportDataSource1.Name = "Auto_1DataSet_Parts";
    
    
    
                reportDataSource1.Value = this.partsBindingSource;
    
    
    
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
    
    
    
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "ReportsApplication.Report2.rdlc";
    
    
    
                this.reportViewer1.Location = new System.Drawing.Point(-3, 48);
    
    
    
                this.reportViewer1.Name = "reportViewer1";
    
    
    
                this.reportViewer1.Size = new System.Drawing.Size(682, 386);
    
    
    
                this.reportViewer1.TabIndex = 0;
    
    
    
    

    this

     

    .Auto_1DataSet.DataSetName = "Auto_1DataSet";

     

     

    this.Auto_1DataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;

     

    this.partsBindingSource.DataMember = "Parts";

     

     

    this.partsBindingSource.DataSource = this.Auto_1DataSet;

     

     

     

     

    this.partsTableAdapter.ClearBeforeFill = true;

     

     

    this.partsTableAdapter.Fill(this.Auto_1DataSet.Parts);

     

     

    this.reportViewer1.RefreshReport();

    this.partsTableAdapter.Fill(this.Auto_1DataSet.Parts); this.reportViewer1.RefreshReport(); } } }

    Sunday, December 13, 2009 11:34 PM

All replies

  • I've optimized the code a bit, and now able to view multiple reports with one control. I have one dataset with all the tables I wish to extract data from, and one bindingSource & one tableAdapter for each table.  My comboBox has 5 items, but only two are shown below.
    private void cboReports_SelectedIndexChanged(object sender, EventArgs e)
            {
                switch (cboReports.SelectedIndex)
                {
                    case 0: printFuelRpt();                    
                        break;
    
                    case 1: printFeesRpt();                    
                        break;
    
                    case 2: printPartsRpt();                    
                        break;
    
                    case 3: printServiceRpt();                    
                        break;
    
                    case 4: printBookmarksRpt();                   
                        break;
                }
            }
    
            private void printFuelRpt()
            {
                reportViewer1.Reset();
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
                reportDataSource1.Name = "AutoDataSet_Fuel";
                reportDataSource1.Value = this.fuelBindingSource;
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "AutoKeeper.Fuel Report.rdlc";
                this.reportViewer1.Location = new System.Drawing.Point(5, 39);
                this.reportViewer1.Name = "reportViewer1";
                this.reportViewer1.Size = new System.Drawing.Size(871, 523);
                this.reportViewer1.TabIndex = 0;
                this.autoDataSet.DataSetName = "AutoDataSet";
                this.autoDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
                this.fuelBindingSource.DataMember = "Fuel";
                this.fuelBindingSource.DataSource = this.autoDataSet;
                this.fuelTableAdapter.ClearBeforeFill = true;
                this.fuelTableAdapter.Fill(this.autoDataSet.Fuel);
                this.reportViewer1.RefreshReport();
            }
    
            private void printFeesRpt()
            {
                reportViewer1.Reset();
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource2 = new Microsoft.Reporting.WinForms.ReportDataSource();
                reportDataSource2.Name = "AutoDataSet_Fees";
                reportDataSource2.Value = this.feesBindingSource;
                this.reportViewer1.LocalReport.DataSources.Add(reportDataSource2);
                this.reportViewer1.LocalReport.ReportEmbeddedResource = "AutoKeeper.Fees Report.rdlc";
                this.reportViewer1.Location = new System.Drawing.Point(5, 39);
                this.reportViewer1.Name = "reportViewer1";
                this.reportViewer1.Size = new System.Drawing.Size(871, 523);
                this.reportViewer1.TabIndex = 0;
                this.autoDataSet.DataSetName = "AutoDataSet";
                this.autoDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
                this.feesBindingSource.DataMember = "Fees";
                this.feesBindingSource.DataSource = this.autoDataSet;
                this.feesTableAdapter.ClearBeforeFill = true;
                this.feesTableAdapter.Fill(this.autoDataSet.Fees);
                this.reportViewer1.RefreshReport();
            }
    
    Thursday, December 17, 2009 3:28 AM