1
private void btnPrint_Click(object sender, EventArgs e)
{    
   Report crt = new Report();    
   DataTable dt = new DataTable();    
   dt = dba.getToForPrint(txtTONumber.Text);    
   dt = dba.getOrderDatails(txtTONumber.Text);    
   crt.SetDataSource(dt);    
   crystalReportViewer1.ReportSource = crt;     
}

我必须调用两种方法来获取数据。我创建了两个数据表,例如 Datatable 和 OrderDetails 。但不能在报表查看器中查看两个表数据,给我任何建议在报表查看器中播种两个数据表?

4

3 回答 3

2

假设我们有两个数据源 dt 和 dt1,并且我们在报表上的两个表中都有字段,然后通过以下方式将两个数据源分配给报表:

// rpt is the object of CrystalDecisions.CrystalReports.Engine.ReportDocument()
   rpt.Database.Tables[0].SetDataSource(dt);
   rpt.Database.Tables[1].SetDataSource(dt1);

我希望它会帮助你。:)

于 2013-06-18T10:59:17.403 回答
0

You first have to set your Crystal Report to use an ADO.NET schema. (see image below). To generate the schema you just create a data set and create tables to fill it. Once filled add your tables (with table names to the data set) then you can export the schema to an xml file.

List<Tuple<string, string>> sqlQueries = new List<Tuple<string, string>>();

// The sql queries below shoudl match the same column names 
// you want to pull back from the database for yoru report
sqlQueries.Add(new Tuple<string, string>("Table1Name", "SELECT TOP 1 .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReportName", "SELECT TOP 1 .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReport2TableName", "SELECT TOP 1 .... FROM ..."));

SqlConnection connection = new SqlConnection(ConnectionString);
DataSet resultSet = new DataSet();

foreach (var tuple in sqlQueries)
{
    SqlDataAdapter adapter = new SqlDataAdapter(tuple.Item1, connection);
    DataTable schema = new DataTable();
    adapter.Fill(schema);
    schema.TableName = tuple.Item2;
    resultSet.Tables.Add(schema);

}

// write out the schema to a file
string path = Path.Combine("PATH_TO_DATASET_XML.xml");
using (var writer = File.CreateText(path))
{
    writer.Write(resultSet.GetXmlSchema().Replace(" encoding=\"utf-16\"", ""));
}

Next use that as a your data source in Crystal Reports

enter image description here

Finally just use the same xml file to fill your report data:

DataSet reportData = new DataSet();
SqlConnection connection = new SqlConnection();
SqlDataAdapter reportAdapter = new SqlDataAdapter();
reportAdapter.SelectCommand = new SqlCommand();
reportAdapter.SelectCommand.Connection = connection;

reportData.ReadXml("PATH_TO_DATASET_XML.xml");

List<Tuple<string, string>> sqlQueries = new List<Tuple<string, string>>();

sqlQueries.Add(new Tuple<string, string>("Table1Name", "SELECT .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReportName", "SELECT .... FROM ..."));
sqlQueries.Add(new Tuple<string, string>("SubReport2TableName", "SELECT .... FROM ..."));

reportData.EnforceConstraints = false;

foreach (var tuple in sqlQueries)
{                
    reportAdapter.SelectCommand.CommandText = tuple.Item1;
    try
    {
        reportAdapter.Fill(reportData, tuple.Item2.Trim());
    }
    catch (Exception ex)
    {
        // Handle your stuff
    }
}

using (var exportReport = new ReportDocument())
{
    exportReport.Load("PATH_TO_RPT_FILE.rpt");
    exportReport.SetDataSource(reportData);

    // export report to wherever you want
}
于 2013-06-18T11:11:32.173 回答
0

您可以使用数据源,也可以在 Crystal 报表中使用两个子报表。将数据表分配给他们每个人作为“水晶”例如

reportDocument.Load(this.MapPath("rptmainReport.rpt"));
reportDocument.OpenSubreport("rptSubReport1.rpt").SetDataSource(dt1);
reportDocument.OpenSubreport("rptSubReport2JNR.rpt").SetDataSource(dt2);

rptSubReport1 和 rptSubReport2 是 mainReport 的子报表。所以您已将数据源设置为子报表

或者

添加虚拟数据列并向它们添加数据,或者根据评论通过视图添加数据源

如果有任何疑问,请随时发表评论

于 2013-06-18T10:45:37.650 回答