使用linq查询作为microsoft本地报告数据源(WinForms)

时间:2011-05-20 07:49:19

标签: c# winforms linq report

如何将linq查询用作本地报表(Microsoft Report)数据源(WinForms)? 我在MyForm中有一个Microsoft Report Viewer Control,我有一个linq查询:

var query = from colV in dal.v_TarafeGharardad join colT in dal.TBL_TarafeGharardad on colV.Id equals colT.PK_Id  select colV;

我想将linq查询用作Microsoft报表数据源。 我试过这个,但我真的知道这是错的。

frmReportViewer ReportViewerForm = new frmReportViewer();
            ReportViewerForm.rptViewer.LocalReport.DataSources.Add(new ReportDataSource("v_TarafeGharardad",query));
            ReportViewerForm.rptViewer.LocalReport.ReportPath = Application.StartupPath + "rptTarafeGharardad.rdlc";
            ReportViewerForm.Show();

解决方案是什么?(谢谢: - *)

更新:如果我可以将linq查询转换为DataTable,问题就解决了。

1 个答案:

答案 0 :(得分:3)

你正在做的事情看起来非常正确。我这样做(这是VB):

rptViewer.LocalReport.ReportPath = <filepath>
rptViewer.LocalReport.DataSources.Clear()
rptViewer.LocalReport.DataSources.Add(New ReportDataSource("<data_source_name_in_report>", <linq_query_result>))
rptViewer.SetDisplayMode(DisplayMode.PrintLayout)

您需要检查的一点是您正确匹配报告中的数据源名称。您可以通过在文本编辑器中打开报告并查看数据源元素来检查这一点。

记忆中的另一件事,我在大约一年内没有用过这个,所以我可能错了,我有一种琐碎的感觉,你必须打电话给rptViewer.Refresh()rptViewer.SetDisplayMode()来强迫它渲染。

有关ReportViewer控件的大量信息,请查看此网站:

http://www.gotreportviewer.com/