為RDLC報表動態繫結資料
阿新 • • 發佈:2019-01-22
C#程式碼
- string conStr = "Data Source=.;Initial Catalog=Book;Integrated Security=True";
- SqlConnection con = new SqlConnection(conStr);
- con.Open();
- SqlDataAdapter da = new SqlDataAdapter("SELECT top 10 * from [dbo].[CuserInfo]", con);
- BookDataSet ds = new BookDataSet(); /*新增的一個DataSet*/
-
/*通過SqlDataAdapter介面卡為DataTable填充(或新增)資料*/
- da.Fill(ds.Tables["CuserInfo"]);
- this.CuserInfoBindingSource.DataSource = ds.Tables["CuserInfo"];
- this.reportViewer1.LocalReport.ReportEmbeddedResource = "MyRDLC.MyReport.rdlc";
- /*方法一:要引用using Microsoft.Reporting.WinForms;名稱空間
- *
-
* ReportDataSource rds = new ReportDataSource("BookDataSet_CuserInfo", ds.Tables["CuserInfo"]);
- * this.reportViewer1.LocalReport.DataSources.Add(rds);
- *
- */
- this.reportViewer1.LocalReport.DataSources.Clear();
- /*方法二*/
- this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("BookDataSet_CuserInfo", ds.Tables["CuserInfo"]));
-
this.reportViewer1.RefreshReport();
其中:BookDataSet_CuserInfo 表示/*資料集名_表名*/ ds.Tables["CuserInfo"] 表示 /*資料來源(表)*/不能為DataSet