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

使用RDLC报表--使用自定义数据集

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

使用RDLC报表--使用自定义数据集

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

使用RDLC报表--使用自定义数据集

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

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

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

使用RDLC报表--使用自定义数据集

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

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

使用RDLC报表--使用自定义数据集

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

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

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

生成数据集:

///<summary>

///报表执行操作

///</summary>

///<paramname="sender"></param>

///<paramname="e"></param>

privatevoidbutton1_Click(objectsender,EventArgse)

{

//取得数据集

stringconnstring="DataSource=.;InitialCatalog=Northwind;IntegratedSecurity=True";

System.Data.SqlClient.SqlConnectionconn1=newSystem.Data.SqlClient.SqlConnection(connstring);

System.Data.SqlClient.SqlCommandcommand1=newSystem.Data.SqlClient.SqlCommand("select*fromcustomers",conn1);

System.Data.SqlClient.SqlDataAdapterada1=newSystem.Data.SqlClient.SqlDataAdapter(command1);

DataSetc_ds=newDataSet();

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(newMicrosoft.Reporting.WinForms.ReportDataSource("myds",c_ds.Tables[0]));

//显示报表

this.reportViewer1.RefreshReport();

}

运行后的数据显示:

使用RDLC报表--使用自定义数据集

相关文章: