使用RDLC报表自定义数据集

<!--[if !supportLists]-->1<!--[endif]-->新建窗体

<!--[if !supportLists]-->2<!--[endif]-->建立数据源

3<!--[endif]-->建立报表

新的数据报表已经生成,下面开始对数据源进行设置。

<!--[if !supportLists]-->4<!--[endif]-->对报表自动生成的数据源进行设置

选择工具栏 à 报表 à 数据源,选中所要修改的数据源后,用“重命名”对其进行修改,如myds。

 

修改完成后,确定退出此窗口。

 

选中报表设计器内的表格,显示属性。将表格的数据集名称更改为上面修改的名称。

如果一个报表文件内只有一个数据源,则表格内的数据值可直接写为“=Fields!字段.Value”的格式,如果包含多个数据源,则要对此字段的取值进行指定,如“=(Fields!字段.value,“数据集名称””。

<!--[if !supportLists]-->5<!--[endif]-->手动生成数据源

手动生成的数据集内必须包含报表文件内设计的字段名称,否则会运行出现错误。

生成数据集:

 

       /// <summary>

        
/// 报表执行操作

        
/// </summary>

        
/// <param name="sender"></param>

        
/// <param name="e"></param>

        
private void button1_Click(object sender, EventArgs e)

         {

            
//取得数据集

            
string connstring = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";

             System.Data.SqlClient.SqlConnection conn1
= new System.Data.SqlClient.SqlConnection(connstring);

             System.Data.SqlClient.SqlCommand command1
= new System.Data.SqlClient.SqlCommand("select * from customers", conn1);

             System.Data.SqlClient.SqlDataAdapter ada1
= new System.Data.SqlClient.SqlDataAdapter(command1);

             DataSet c_ds
= new DataSet();

            
try

             {

                 conn1.Open();

                 ada1.Fill(c_ds);

             }

            
finally

             {

                 conn1.Close();

                 command1.Dispose();

                 conn1.Dispose();

             }



            
//为报表浏览器指定报表文件

            
this.reportViewer1.LocalReport.ReportEmbeddedResource = "report.Report1.rdlc";

            
//指定数据集,数据集名称后为表,不是DataSet类型的数据集

            
this.reportViewer1.LocalReport.DataSources.Clear();

            
this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("myds", c_ds.Tables[0]));

            
//显示报表

            
this.reportViewer1.RefreshReport();

         }

 

运行后的数据显示:

posted @ 2008-07-13 20:20  梦极无边界  阅读(2362)  评论(1编辑  收藏  举报