简体   繁体   中英

How to load two data table to same Crystal report in c#?

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;     
}

I have to call two method to get data. I created two datatables such as Datatable and OrderDetails . but can not vie both of tables data in report viewer, give me any suggestion to sow both datatables in reportviewer ?

Suppose we have two datasources dt and dt1 and we have fields from both the tables on the report then to assign both the data sources to report in the following way:

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

I hope it will help you. :)

Either you can use datasource or by having two subreport in Crystal report. assign datatable to each of them as "Crystal" For example

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

rptSubReport1 and rptSubReport2 are sub reports of mainReport .So You have set datasource to subreport

OR

Add dummy datacolumn and add data to them or as per comment through view you can add datasource

If any query feel free to comment

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

在此处输入图片说明

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM