简体   繁体   English

在数据集中使用多个数据表

[英]Use more than one Datatable in Dataset

I have to create a crystal report , whose data will be populated from Dataset. 我必须创建一个Crystal报表,其数据将从Dataset中填充。 Dataset has three datatable. 数据集具有三个数据表。 Namely: 即:

CustDetais CustDetais
BookingDetails 预订详情
FoodNExtra FoodNExtra

When I setup crystal report through Crystal Report Wizard , I got new screen which says 当我通过Crystal Report向导设置Crystal Report时,出现新屏幕,显示 在此处输入图片说明 Not sure what to do here so I just clicked next. 不知道在这里做什么,所以我单击了下一步。 And heres my code for crystal report viewer : 这是我的水晶报表查看器代码:

private void CRKOTQoute_Load(object sender, EventArgs e)
{
    try
    {
        MySqlCommand cmd = new MySqlCommand("select CustName,Phone,Address,Email from tblCustDetails where custid=@custid", con.con);
        cmd.Parameters.AddWithValue("@custid", BLDashboard.custid);
        MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
        DataSet1 ds = new DataSet1();
        adapter.Fill(ds, "CustDetais");
        if (ds.Tables["CustDetais"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        MySqlCommand cmd1 = new MySqlCommand("select BookingID,BookingDate,Event,EventDate,EventTime,Pax,Service,ServiceTime from tblBookingDetails where BookingID=@Bookid", con.con);
        cmd.Parameters.AddWithValue("@bookid", BLDashboard.bookingID);
        MySqlDataAdapter adapter1 = new MySqlDataAdapter(cmd1);
        DataSet1 ds1 = new DataSet1();
        adapter.Fill(ds1, "BookingDetails");
        if (ds1.Tables["BookingDetails"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        MySqlCommand cmd2 = new MySqlCommand("select FoodMenu,ExtraItem from tblItem where BookingID=@Bookid1", con.con);
        cmd.Parameters.AddWithValue("@bookid1", BLDashboard.bookingID);
        MySqlDataAdapter adapter2 = new MySqlDataAdapter(cmd2);
        DataSet1 ds2 = new DataSet1();
        adapter.Fill(ds2, "BookingDetails");
        if (ds2.Tables["FoodNExtra"].Rows.Count == 0)
        {
            MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        RPTKOTQoute printKOTqoute = new RPTKOTQoute();
        //RPTKitchenQoute printKOTqoute = new RPTKitchenQoute();
        //RPTKOTTest printKOTqoute = new RPTKOTTest();
        printKOTqoute.SetDataSource(ds);
        printKOTqoute.SetDataSource(ds1);
        printKOTqoute.SetDataSource(ds2);
        crystalReportViewer1.ReportSource = printKOTqoute;
        System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
        printKOTqoute.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;
        printKOTqoute.PrintOptions.PrinterName = "EPSON TM-U220 Receipt";
        printKOTqoute.PrintToPrinter(1, false, 0, 0);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

I used same way as mentioned here . 我使用这里提到的相同方法。

And when I run crystal report theres no error but no data is displayed. 当我运行Crystal Report时,没有错误,但是没有数据显示。 I tried using just One datatable and it worked fine. 我尝试仅使用一个数据表,但效果很好。 Also I am using MySQL as database. 另外,我正在使用MySQL作为数据库。

Try and merge the three datasets into one before assigning them to the report data source: 将它们分配给报表数据源之前,请尝试将这三个数据集合并为一个:

ds.Merge(ds1)
ds.Merge(ds2)

This way, ds should include all the data (all tables) in ds , ds1 and ds2 . 这样,ds应该包括dsds1ds2所有数据(所有表)。 Then, assign only ds as the DataSource of the report. 然后,仅将ds分配为报表的数据源。

I think that Crystal Reports supports only one SELECT statement as data source per report, (at least this is the case in Crystal Reports 8.5 UI which I have used), if you use more its behaviour is a bit unpredictable. 我认为Crystal Reports仅支持一个SELECT语句作为每个报表的数据源(至少在我使用过的Crystal Reports 8.5 UI中是这种情况),如果您使用更多,其行为将是无法预测的。 That is probably why the wizard asks from you to join the tables. 这可能就是向导要求您加入表的原因。 If a join query that would bring all the data wanted is not a solution for you, then probably the only solution would be to add subreports in your report. 如果可以提供所有所需数据的联接查询不是您的解决方案,那么唯一的解决方案可能是在报表中添加子报表。 But still you cannot add them programmatically, you should add them in design mode, and then attach the datasets as datasources via code like this: 但是仍然不能以编程方式添加它们,应该在设计模式下添加它们,然后通过如下代码将数据集作为数据源附加:

printKOTqoute.Subreports[0].SetDataSource(ds);
printKOTqoute.Subreports[1].SetDataSource(ds1);
printKOTqoute.Subreports[2].SetDataSource(ds2);

Also check this out How to set datasource of Sub crystal report in c# win form app 还要检查一下如何在C#Win Form应用程序中设置Sub Crystal报表的数据源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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