为RDLC报表动态绑定数据

来源:互联网 发布:台湾网络电视 编辑:程序博客网 时间:2024/06/06 00:14
C#代码  收藏代码
  1. string conStr = "Data Source=.;Initial Catalog=Book;Integrated Security=True";  
  2. SqlConnection con = new SqlConnection(conStr);  
  3. con.Open();  
  4. SqlDataAdapter da = new SqlDataAdapter("SELECT  top 10 * from [dbo].[CuserInfo]", con);  
  5. BookDataSet ds = new BookDataSet(); /*添加的一个DataSet*/  
  6. /*通过SqlDataAdapter适配器为DataTable填充(或添加)数据*/  
  7. da.Fill(ds.Tables["CuserInfo"]);   
  8. this.CuserInfoBindingSource.DataSource = ds.Tables["CuserInfo"];  
  9. this.reportViewer1.LocalReport.ReportEmbeddedResource = "MyRDLC.MyReport.rdlc";  
  10. /*方法一:要引用using Microsoft.Reporting.WinForms;命名空间 
  11.  * 
  12.  * ReportDataSource rds = new ReportDataSource("BookDataSet_CuserInfo", ds.Tables["CuserInfo"]); 
  13.  * this.reportViewer1.LocalReport.DataSources.Add(rds); 
  14.  * 
  15.  */  
  16. this.reportViewer1.LocalReport.DataSources.Clear();   
  17. /*方法二*/                                                                                               
  18. this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("BookDataSet_CuserInfo", ds.Tables["CuserInfo"]));  
  19. this.reportViewer1.RefreshReport();  

 其中:BookDataSet_CuserInfo 表示/*数据集名_表名*/          ds.Tables["CuserInfo"] 表示 /*数据源(表)*/不能为DataSet