日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在代码中向ReportViewer动态添加数据源

發布時間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在代码中向ReportViewer动态添加数据源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于ReportViewer控件的使用,網上很多文章介紹都是直接使用ReportViewer控件右上方的三角按鈕來綁定數據源和表,感覺不是很方便,那能不能動態來進行綁定呢,我做了個實驗,暫時沒發現什么問題。記錄如下:

首先,仍然是使用VS中添加項的方式添加了兩個DataSet,分別命名為DataSet1和DataSet2,它們都有一個DataTable,分別是DataTable1和DataTable2。每個DataTable上都添加了一個TableAdaptor,分別是DataTable1TableAdaptor1和DataTable2TableAdaptor2。

其次,仍然使用VS中添加項的方式針對兩個數據源都添加了一個報表,分別為Report1.rdlc和Report2.rdlc,對這兩個表分別進行界面設計。

在主窗體,添加一個Button1,目的是點擊Button1后加載報表內容,并添加一個ReportViewer對象,其名稱為reportViewer1。

動態生成的對象需包括:BindingSource, DataSet和TableAdapter,另外,在設置這些對象的名字域時要注意按注釋里的說明進行書寫,否則加載報表顯示時可能出現“尚未為數據源XXX提供數據源實例”的錯誤提示。

以下例子的目的是點擊Button1后加載DataSet1中DataTable1的相關數據,報表

???????? private void button1_Click(object sender, EventArgs e)
??????? {

?????????? BindingSource tBindingSource = new BindingSource();
??????????? WindowsApplication3.DataTable1TableAdaptor1.sqlInfoTableAdapter ds1TableAdapter=new WindowsApplication3.DataTable1TableAdaptor1.sqlInfoTableAdapter (); /*直接new一個DataTable1中已經創建好的Adapter*/
??????????? WindowsApplication3.DataSet1 tDataSet = new WindowsApplication3.DataSet1();
??????????? tDataSet.DataSetName = "tDataSet"; /*必須是本DataSet變量的名字*/
??????????? tDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
??????????? tBindingSource.DataMember = "DataTable1";/*必須是所綁定的數據源DataTable的名字*/
??????????? tBindingSource.DataSource = tDataSet;
??????????? tBindingSource.Position = 0;
??????????? ds1TableAdapter.ClearBeforeFill = true;
??????????? ReportDataSource rds = new ReportDataSource();
??????????? rds.Name = "DataSet1_DataTable1";/*必須是數據源DataSet名_數據源DataTable名*/
??????????? rds.Value = tBindingSource;
??????????? this.reportViewer1.Reset();
??????????? this.reportViewer1.LocalReport.DataSources.Clear();
??????????? this.reportViewer1.LocalReport.DataSources.Add(rds);
??????????? this.reportViewer1.LocalReport.ReportEmbeddedResource = "WindowsApplication3.Report1.rdlc";/*必須是相應報表rdlc文件在工程中的全路徑名*/
??????????? ReportParameter rp = new ReportParameter("begin_time", this.dateTimePicker1.Value.ToString());
??????????? this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp });
??????????? ds1TableAdapter.Fill(tDataSet.viewG4OvenInfo, dateTimePicker1.Value);
??????????? this.reportViewer1.RefreshReport();

}

?

同理,在button2中可增加對DataSet2的數據源的綁定。

通過點擊不同的按鈕,ReportViewer控件中能夠顯示不同的表內容

轉載于:https://blog.51cto.com/1117865/642686

總結

以上是生活随笔為你收集整理的在代码中向ReportViewer动态添加数据源的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。