ReportViewer的动态绑定
aspx页面关键代码 代码
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<uc2:StatementQuery ID="StatementQuery1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="View Report" />
<asp:Button
ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
<asp:TextBox ID="txtParameter" runat="server"></asp:TextBox>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt"
>
<LocalReport ReportPath="statement/Report_Metal.rdlc" >
<DataSources>
<rsweb:ReportDataSource />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
</form>
</body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<uc2:StatementQuery ID="StatementQuery1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="View Report" />
<asp:Button
ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
<asp:TextBox ID="txtParameter" runat="server"></asp:TextBox>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt"
>
<LocalReport ReportPath="statement/Report_Metal.rdlc" >
<DataSources>
<rsweb:ReportDataSource />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
</form>
</body>
C#关键代码
代码
private void BindReportViewer()
{
BusinessLogic.MetalStatViewCollection MSVCOLL = new BusinessLogic.MetalStatViewCollection();
MSVCOLL = BusinessLogic.MetalStatViewCollection.getMetalStatViewCollection("year=2009 and month=12 and day=3");
IList<rMSV> MSVList = new List<rMSV>();
foreach (MetalStatView m in MSVCOLL)
{
MSVList.Add(new rMSV(m.GroupField, m.clientName, m.weight, m.amount));
}
ReportDataSource rds = new ReportDataSource("DataSet_Metal_DataTableMetal", MSVList);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}
{
BusinessLogic.MetalStatViewCollection MSVCOLL = new BusinessLogic.MetalStatViewCollection();
MSVCOLL = BusinessLogic.MetalStatViewCollection.getMetalStatViewCollection("year=2009 and month=12 and day=3");
IList<rMSV> MSVList = new List<rMSV>();
foreach (MetalStatView m in MSVCOLL)
{
MSVList.Add(new rMSV(m.GroupField, m.clientName, m.weight, m.amount));
}
ReportDataSource rds = new ReportDataSource("DataSet_Metal_DataTableMetal", MSVList);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}
需要用dataset的XSD文件作为中转。
详细过程,可以参考:http://www.codeproject.com/kb/aspnet/ReportViewer.aspx
以前按照MSDN的指导,有成功使用过动态绑定RDLC,可这次使用到业务对象,并用ObjectDataSource来作为数据源的中转,结果搞了几天,也总是有问题。
后来,重头来过,不用ObjectDataSource,按codeproject上的简单例子,实现了动态绑定,再将我们要使用的业务对象加上去,通过XSD来中转,一样完成了目标功能。
http://four-corner.appspot.com/