如何在Crystal Report中创建销售发票报表(在两个表中绑定数据)

时间:2015-12-30 05:14:55

标签: c# wpf crystal-reports-2010

我需要创建一个水晶报表来显示两个表中的数据(即Sales_Invoice和Sales_Invoice_Items).Sales_Invoice_Items有一个来自Sales_Invoice的外来。我需要显示这样的报告,

enter image description here

我创建了一个Crystal报表来显示这样的数据。 Sales_Invoice表中的数据相对于invoice_no正确分组。但它显示每个Sales_Invoice行中Sales_Invoice_Items表中的所有数据。我需要在Sales_Invoice_Items中显示Sales_Invoice的外来Sales_Invoice_Items数据

enter image description here

然后我用以下代码绑定它。

   private DataSet getData()
    {
        DataSetSalesReport ds = new DataSetSalesReport();
        try
        {
            using (SqlConnection con = new SqlConnection(DBCon.conStr))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(AppConstraints.GET_SALES_REPORT);
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataReader rd = cmd.ExecuteReader();
                decimal slno = 1;
                while (rd.Read())
                {
                    DataRow dr = ds.Tables["DataTable1"].NewRow();
                    dr["Id"] = rd["Id"].ToString();
                    dr["invoiceno"] = rd["invoiceno"].ToString();
                    dr["invoice_date"] = rd["invoice_date"].ToString();
                    dr["delivery_date"] = rd["delivery_date"].ToString();
                    dr["account"] = rd["account"].ToString();
                    dr["customer"] = rd["customer"].ToString();
                    dr["gross_amount"] = rd["gross_amount"].ToString();
                    dr["discount"] = rd["discount"].ToString();
                    dr["total_tax"] = rd["total_tax"].ToString();
                    dr["net_amount"] = rd["net_amount"].ToString();
                    dr["cash_discount"] = rd["cash_discount"].ToString();
                    dr["freight_charge"] = rd["freight_charge"].ToString();
                    dr["additional"] = rd["additional"].ToString();
                    dr["paid"] = rd["paid"].ToString();
                    dr["balance"] = rd["balance"].ToString();
                    dr["balancepaid_date"] = rd["balancepaid_date"].ToString();
                    dr["note"] = rd["note"].ToString();
                    dr["total_amount"] = rd["total_amount"].ToString();
                    dr["payment_mode"] = rd["payment_mode"].ToString();
                    dr["ac"] = rd["ac"].ToString();
                    dr["reg_date"] = rd["reg_date"].ToString();
                    dr["last_update"] = rd["last_update"].ToString();
                    dr["invoice_note"] = rd["invoice_note"].ToString();
                    dr["slno"] = slno++.ToString();
                    ds.Tables["DataTable1"].Rows.Add(dr);

                    using (SqlConnection con1 = new SqlConnection(DBCon.conStr))
                    {
                        con1.Open();
                        SqlCommand cmd1 = new SqlCommand(AppConstraints.GET_SALES_ITEMS_REPORT);
                        cmd1.Connection = con1;
                        cmd1.CommandType = CommandType.StoredProcedure;
                        cmd1.Parameters.AddWithValue("@Id", rd["Id"].ToString());
                        SqlDataReader rd1 = cmd1.ExecuteReader();
                        decimal j = 1;
                        while(rd1.Read())
                        {
                            DataRow dr1 = ds.Tables["DataTable2"].NewRow();
                            dr1["Id"] = rd1["Id"].ToString();
                            dr1["invoice_id"] = rd["Id"].ToString();
                            dr1["item_name"] = rd1["item_name"].ToString();
                            dr1["barcode"] = rd1["barcode"].ToString();
                            dr1["rate"] = rd1["rate"].ToString();
                            dr1["discount"] = rd1["discount"].ToString();
                            dr1["qty"] = rd1["qty"].ToString();
                            dr1["unit"] = rd1["unit"].ToString();
                            dr1["tax"] = rd1["tax"].ToString();
                            dr1["net"] = rd1["net"].ToString();
                            dr1["mrp"] = rd1["mrp"].ToString();
                            dr1["slno"] = j++.ToString();

                            ds.Tables["DataTable2"].Rows.Add(dr1);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        return ds;
    }

我在WPF窗口的Window_Loaded事件中调用上面的方法

     private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ReportDocument report = new ReportDocument();
        report.Load("rptSalesReport.rpt");
        report.SetDataSource(getData());
        PrintSalesReport.ViewerCore.ReportSource = report;
        PrintSalesReport.ToggleSidePanel = SAPBusinessObjects.WPF.Viewer.Constants.SidePanelKind.None;
    }

我的数据集如下所示

enter image description here

运行后我得到以下结果

enter image description here

以上结果;它显示每个Sales_Invoice数据中的所有Sales_invoice_Items数据。

我需要显示与Sales_Invoice_Items数据相关的Sales_Invoice数据(Sales_Invoice_Items包含Sales_Invoice的外键)。我需要您的帮助才能执行此操作。

谢谢

1 个答案:

答案 0 :(得分:0)

使用子报告解决此问题enter image description here