1

我正在尝试将 C# 代码中的值传递给 CrystalReport 报表。

编辑

private void PrintOrder(List<OrderPrintBO> pListOrderBO)
        {
            DSOrderReport oDSOrderReport = new DSOrderReport();
            DataTable oDataTable = oDSOrderReport.Tables[0];
            String sOrderNo = "";

            if (pListOrderBO.Count > 0)
            {
                for (int i = 0; i < pListOrderBO.Count; i++)
                {
                    DataRow oRow = oDataTable.NewRow();
                    oRow["OrderID"] = pListOrderBO[i].OrderID;
                    oRow["OrderNumber"] = pListOrderBO[i].OrderNumber;
                    sOrderNo = pListOrderBO[i].OrderNumber;
                    oDataTable.Rows.Add(oRow);
                }
            }

            oDSOrderReport.Merge(oDataTable);
            oDSOrderReport.AcceptChanges();

            if (oDSOrderReport.Tables[0].Rows.Count > 0)
            {
                // Main Copy
                PrintDialog printDialog = new PrintDialog();
                rptOrder oMainOrder = new rptOrder();

                String sCompanyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
                String sPhone1 = System.Configuration.ConfigurationManager.AppSettings["Phone1"].ToString();
                String sPhone2 = System.Configuration.ConfigurationManager.AppSettings["Phone2"].ToString();
                String sShowOrderNo = System.Configuration.ConfigurationManager.AppSettings["ShowOrderNo"].ToString();

                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtCompanyName"]).Text = sCompanyName;
                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtPhone1"]).Text = "Tel:" + sPhone1;
                ((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtPhone2"]).Text = "Tel:" + sPhone2;

////这个注释掉的部分给出了异常

 //string sVarOrderNo = "";

            //if (sShowOrderNo.ToLower() == "yes")
            //{
            //    sVarOrderNo = sOrderNo;
            //}


            //((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtOrderNo"]).Text = "O.N. : " + sVarOrderNo;

                oMainOrder.SetDataSource(oDSOrderReport);

                oMainOrder.PrintOptions.PrinterName = printDialog.PrinterSettings.PrinterName;


    try
                    {
                        oMainOrder.PrintToPrinter(1, false, 0, 0);
 MessageBox.Show("Order Printed Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("Error in Printing Order", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

结束编辑

对于前三个文本框(公司名称、电话 1 和电话 2),一切正常,但是当我尝试将 OrderNo 传递给报告时,索引超出了数组的范围。发生异常,即使我的报告有一个名为txtOrderNo的 TextBox 对象

我不确定为什么会这样。请帮忙。谢谢。

4

1 回答 1

0

尝试更改您的sOrderNo相关代码如下。

string sVarOrderNo = "";

if (sShowOrderNo.ToLower() == "yes")
{
     sVarOrderNo =  sOrderNo;
}


((CrystalDecisions.CrystalReports.Engine.TextObject)oMainOrder.ReportDefinition.ReportObjects["txtOrderNo"]).Text = "O.N. : " + sVarOrderNo;

更新

您的问题是下面提到的代码片段。在for loop您尝试将值分配给sOrderNo. 这是没有意义的。B'cos 每次循环时都会覆盖它。那么这样做的目的是什么?如果您需要此值,则必须将此值作为表格的行或作为报告的参数。

if (pListOrderBO.Count > 0)
            {
                for (int i = 0; i < pListOrderBO.Count; i++)
                {
                    DataRow oRow = oDataTable.NewRow();
                    oRow["OrderID"] = pListOrderBO[i].OrderID;
                    oRow["OrderNumber"] = pListOrderBO[i].OrderNumber;
                    sOrderNo = pListOrderBO[i].OrderNumber;
                    oDataTable.Rows.Add(oRow);
                }
            }

我希望这对你有帮助。

于 2013-01-27T07:56:26.857 回答