Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

mysql - How to bind datatable to reportviewer runtime

I want to do a bind datatable to reportviewer with the code below. I don't see the results appear in the reportviewer, what the lack of 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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the answer how to bind datatable to reportviewer, I will shared here may be useful for others.

  1. Add to form clsTables class, Report1.rdlc file, reportViewer1.
  2. Then Click on the upper right corner of the reportViewer1, set choose report to Test.Report1.rdlc.
  3. On Report1.rdlc
    • Click New,
    • Add dataset name: dsBody Data source: Test Available dataset: clsTables
    • Click Ok
    • Right click on Report1.rdlc select Insert Table, drag dsBody element(Column0, Colum1, Column2) to Report1.rdlc table.
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();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...