How to bind data to reportviewer runtime

I want to make datatable binding to reportviewer using the code below. I do not see the results being displayed in the reportviewer , what is the below script below?

// create dataset
DataSet ds = new DataSet("myDataset");

// create datatable
DataTable dt = new DataTable("myDatatable");

// add columns
dt.Columns.Add("column1", typeof(string));
dt.Columns.Add("column2", typeof(string));
dt.Columns.Add("column3", typeof(string));

// insert data rows
dt.Rows.Add("row1-col1", "row1-col2", "row1-col3");
dt.Rows.Add("row2-col1", "row2-col2", "row2-col3");

// add datatable to dataset 
ds.Tables.Add(dt);

// bind datatable to report viewer
this.reportViewer.Reset();
this.reportViewer.ProcessingMode = ProcessingMode.Local;
this.reportViewer.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc";
this.reportViewer.LocalReport.DataSources.Clear();
this.reportViewer.LocalReport.DataSources.Add(new ReportDataSource(dt.TableName, dt));
this.reportViewer.RefreshReport();
+5
source share
3 answers

I found the answer how to bind datatable to reportviewer, I will share here, it can be useful for others.

  • Add the clsTables class to the form , the file Report1.rdlc , reportViewer1 .
  • reportViewer1, Test.Report1.rdlc.
  • Report1.rdlc
    • ,
    • : dsBody : : clsTables
    • Report1.rdlc, , dsBody (Column0, Colum1, Column2) Report1.rdlc.
Namespace Test{
    public class clsTables {
        // constructor
        public clsTables(string col0, string col1, string col2) {
            this.Column0= col0;
            this.Column1= col1;
            this.Column2= col2;
        }

       // properties
       public string Column0{ get; set; }
       public string Column1{ get; set; }
       public string Column2{ get; set; }
    }
}

namespace Test{
    public class clsMain{
        public void BindToRepprtViewer() {        
            // create dataset
            DataSet ds = new DataSet("myDataset");

            // create datatable
            DataTable dt = new DataTable("myDatatable");

            // add columns
            dt.Columns.Add("column1", typeof(string));
            dt.Columns.Add("column2", typeof(string));
            dt.Columns.Add("column3", typeof(string));

            // insert data rows
            dt.Rows.Add("row1-col1", "row1-col2", "row1-col3");
            dt.Rows.Add("row2-col1", "row2-col2", "row2-col3");

            // add datatable to dataset 
            ds.Tables.Add(dt);

            // save rows to rowList 
            List<clsTables> rowList = new List<clsTables>();
            rowList .Clear();
            foreach (DataRow row in dt.Rows) {
                rList.Add(new clsTables(Convert.ToInt32(row.ItemArray[0]), row.ItemArray[1].ToString(), row.ItemArray[2].ToString()));
            }

            // binding rowList to bs
            BindingSource bs = new BindingSource();
            bs.DataSource = rowList;

            // binding bs to rds
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "dsBody";
            rds.Value = bs;

            // binding rds to report viewer
            reportViewer1.Reset();
            reportViewer1.LocalReport.ReportEmbeddedResource =  "Test.Report1.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(rds);
            reportViewer1.RefreshReport();
        }
    }
}
+6

, -, - :

reportViewer1.LocalReport.DataSources.Clear();

, a:

reportViewer1.LocalReport.Refresh(); 

RefreshReport() Reportviewer.

, ReportEmbeddedResource, , .

0

, , Googling, . , . :

  • DataSet (.. MyDataSet).
  • DataTable DataSet (MyDataTable).
  • .
  • (Report1.rdlc).
  • MyDataSet.
  • ReportViewer (reportViewer).
  • Click the arrow in the upper right corner and select Report1.rdlc. This will automatically add the DataSet and BindingSource to your form.
  • Do the following in your form load event (or where ever makes sense):
MyDataSetBindingSource.DataSource = GetDataTable();
reportViewer.RefreshReport();

Make sure the column names in your DataTable match the ones you defined in MyDataTable.

0
source

All Articles