Dynamically bind rdlc Report to Reportviewer.
-
Thursday, April 19, 2012 11:11 AM
Hi I am new in programing.
I have created 2 rdlc reports Report1.rdlc and Report2.rdlc I want to use a combobox1 in the form1 to list report1 and report2. When user select a report, it automatically populate the content of the chosen report in the reportviewer1.
I am using Report under C# Visual Studio 2010.
Here is my code butwhen I pull the form1 up, it is empty and the combobox is empty aswell.
Form 1 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ReportsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource(); private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { reportViewer1.Reset(); switch(comboBox1.SelectedItem.ToString()) { case "Report1": reportDataSource.Name = "Reports_GetReport1Report"; reportDataSource.Value = igoDataSet1 ; reportViewer1.LocalReport.DataSources.Add(reportDataSource); reportViewer1.LocalReport.ReportEmbeddedResource = "Reportsapplication5.Report1.rdlc"; reportViewer1.RefreshReport(); break; case "Report2": reportDataSource.Name = "Reports_GetReport2Report"; reportDataSource.Value = igoDataSet1; reportViewer1.LocalReport.DataSources.Add(reportDataSource); reportViewer1.LocalReport.ReportEmbeddedResource = "Reportsapplication5.Report2.rdlc"; reportViewer1.RefreshReport(); break; } } } }
namespace ReportsApplication5 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); Microsoft.Reporting.WinForms.ReportDataSource reportDataSource3 = new Microsoft.Reporting.WinForms.ReportDataSource(); this.igoDataSet1 = new ReportsApplication5.igoDataSet1(); this.tableAdapterManager = new ReportsApplication5.igoDataSet1TableAdapters.TableAdapterManager(); this.reportViewer1 = new Microsoft.Reporting.WinForms.ReportViewer(); this.LabelsbyzipTableAdapter = new ReportsApplication5.igoDataSet1TableAdapters.LabelsbyzipTableAdapter(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.igoDataSet1BindingSource = new System.Windows.Forms.BindingSource(this.components); this.labelsbyzipBindingSource1 = new System.Windows.Forms.BindingSource(this.components); this.labelsbyzipBindingSource = new System.Windows.Forms.BindingSource(this.components); ((System.ComponentModel.ISupportInitialize)(this.igoDataSet1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.igoDataSet1BindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.labelsbyzipBindingSource1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.labelsbyzipBindingSource)).BeginInit(); this.SuspendLayout(); // // igoDataSet1 // this.igoDataSet1.DataSetName = "igoDataSet1"; this.igoDataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // tableAdapterManager // this.tableAdapterManager.BackupDataSetBeforeUpdate = false; this.tableAdapterManager.Connection = null; this.tableAdapterManager.UpdateOrder = ReportsApplication5.igoDataSet1TableAdapters.TableAdapterManager.UpdateOrderOption.InsertUpdateDelete; // // reportViewer1 // reportDataSource3.Name = "DataSet1"; reportDataSource3.Value = null; this.reportViewer1.LocalReport.DataSources.Add(reportDataSource3); this.reportViewer1.LocalReport.ReportEmbeddedResource = "ReportsApplication5.Report1.rdlc"; this.reportViewer1.Location = new System.Drawing.Point(12, 69); this.reportViewer1.Name = "reportViewer1"; this.reportViewer1.Size = new System.Drawing.Size(658, 305); this.reportViewer1.TabIndex = 0; // // LabelsbyzipTableAdapter // this.LabelsbyzipTableAdapter.ClearBeforeFill = true; // // comboBox1 // this.comboBox1.DataSource = this.labelsbyzipBindingSource; this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(158, 27); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(311, 21); this.comboBox1.TabIndex = 1; // // igoDataSet1BindingSource // this.igoDataSet1BindingSource.DataSource = this.igoDataSet1; this.igoDataSet1BindingSource.Position = 0; // // labelsbyzipBindingSource1 // this.labelsbyzipBindingSource1.DataMember = "Labelsbyzip"; this.labelsbyzipBindingSource1.DataSource = this.igoDataSet1BindingSource; // // labelsbyzipBindingSource // this.labelsbyzipBindingSource.DataMember = "Labelsbyzip"; this.labelsbyzipBindingSource.DataSource = this.igoDataSet1BindingSource; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(682, 386); this.Controls.Add(this.comboBox1); this.Controls.Add(this.reportViewer1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.igoDataSet1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.igoDataSet1BindingSource)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.labelsbyzipBindingSource1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.labelsbyzipBindingSource)).EndInit(); this.ResumeLayout(false); } #endregion private igoDataSet1 igoDataSet1; private igoDataSet1TableAdapters.TableAdapterManager tableAdapterManager; private Microsoft.Reporting.WinForms.ReportViewer reportViewer1; private igoDataSet1TableAdapters.LabelsbyzipTableAdapter LabelsbyzipTableAdapter; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.BindingSource igoDataSet1BindingSource; private System.Windows.Forms.BindingSource labelsbyzipBindingSource1; private System.Windows.Forms.BindingSource labelsbyzipBindingSource; } }

