Set DataTable as DataSource for Crystal Report

2019-09-16 13:08发布

问题:

I am creating a report in Visual Studio 2008. My crystal report is created using TTX or Data Definition File and I am passing a DataTable as its data source. I have already checked my TTX and DataTable columns if they are the same.

Here is the code:

string strReportFilePath = ConfigurationManager.AppSettings["ReportsPath"] + "MyReport.rpt";
rpt.Load(strReportFilePath);
DataTable dt = GetDataTableFromOracle("select item_no, descr from items");
crvReportViewer.ReportSource = rpt;
crvReportViewer.DataBind();

The result is no data on crystal report. Am I missing something? What's wrong with my code?

回答1:

Adding TableName will solve this problem.

string strReportFilePath = ConfigurationManager.AppSettings["ReportsPath"] + "MyReport.rpt";
rpt.Load(strReportFilePath);
DataTable dt = GetDataTableFromOracle("select item_no, descr from items");
dt.TableName = "FileNameOfTheTTX";
crvReportViewer.ReportSource = rpt;
crvReportViewer.DataBind();