【问题标题】:Use more than one Datatable in Dataset在数据集中使用多个数据表
【发布时间】:2017-05-05 12:57:36
【问题描述】:

我必须创建一个水晶报表,其数据将从数据集中填充。数据集有三个数据表。 即:

客户详情
预订详情
FoodNExtra

当我通过水晶报表向导设置水晶报表时,我得到了一个新屏幕,上面写着 不知道在这里做什么,所以我点击了下一步。 这是我的水晶报表查看器代码:

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

我使用了与here相同的方式。

当我运行水晶报告时,没有错误,但没有显示数据。 我尝试只使用一个数据表,它工作正常。 我也使用 MySQL 作为数据库。

【问题讨论】:

    标签: c# mysql datatable crystal-reports


    【解决方案1】:

    尝试将三个数据集合并为一个,然后再将它们分配给报表数据源:

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

    这样,ds 应该包含dsds1ds2 中的所有数据(所有表)。然后,仅指定 ds 作为报表的 DataSource。

    【讨论】:

      【解决方案2】:

      我认为 Crystal Reports 仅支持一个 SELECT 语句作为每个报表的数据源(至少在我使用的 Crystal Reports 8.5 UI 中是这种情况),如果您使用更多,它的行为有点不可预测。这可能就是向导要求您加入表格的原因。如果可以带来所有所需数据的连接查询不是您的解决方案,那么可能唯一的解决方案是在您的报表中添加子报表。但是您仍然不能以编程方式添加它们,您应该在设计模式下添加它们,然后通过如下代码将数据集作为数据源附加:

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

      也可以看看How to set datasource of Sub crystal report in c# win form app

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 1970-01-01
        • 2015-12-12
        相关资源
        最近更新 更多