
























基本上报表的使用就是这样了。 附上一个步骤4的代码。
public partial class OrderReport : Form
{

private ReportDocument reportDoc = new ReportDocument();
//private string Path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf(@"\"));
private string Path = Application.StartupPath;

private int _orderId = 0;
public int OrderId
{
get { return _orderId; }
set { _orderId = value; }
}

public OrderReport()
{
InitializeComponent();
}

private void crystalReportViewer1_Load(object sender, EventArgs e)
{
ReportDAL reportingDAL = new ReportDAL();

CompanysTableAdapter companyAdt = new CompanysTableAdapter();
ProductsTableAdapter productAdt = new ProductsTableAdapter();
OrdersTableAdapter orderAdt = new OrdersTableAdapter();
OrderItemsTableAdapter orderitemAdt = new OrderItemsTableAdapter();

Path = Path + @"\Report\";

try
{
string reportPath = Path + "OrderReport.rpt";
reportDoc.Load(reportPath);

// Get Order
OrderDs.OrdersDataTable orderTable = orderAdt.GetOneOrder(OrderId);
if (orderTable.Count < 1)
{
MessageBox.Show("订单不存在或订单信息读取失败!", "打印送货单", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
reportingDAL.Orders.ImportRow(orderTable[0]);
reportingDAL.Orders[0].OrderName = "NO:GB-2008-06-" + orderTable[0].OrderId.ToString();
reportingDAL.Orders[0].PriceName = ChineseNum.GetUpperMoney(Convert.ToDouble(orderTable[0].Price));

// Get Company Infos
CompanyDs.CompanysDataTable companyTable = companyAdt.GetOneCompany(orderTable[0].CompanyId);

reportingDAL.Companys.ImportRow(companyTable[0]);

// Get Order Items Infos
OrderDs.OrderItemsDataTable orderitemTable = orderitemAdt.GetOrderItemsByOrderId(OrderId);
ProductDs.ProductsDataTable productTable;
foreach (OrderDs.OrderItemsRow row in orderitemTable)
{
reportingDAL.OrderItems.ImportRow(row);
productTable = productAdt.GetOneProduct(row.ProductId);
reportingDAL.OrderItems[reportingDAL.OrderItems.Count - 1].ProductName = productTable[0].ProductName;
reportingDAL.OrderItems[reportingDAL.OrderItems.Count - 1].Code = productTable[0].Code;
reportingDAL.OrderItems[reportingDAL.OrderItems.Count - 1].Price = productTable[0].Price.ToString("F2");
reportingDAL.OrderItems[reportingDAL.OrderItems.Count - 1].Spec = productTable[0].Spec;
reportingDAL.OrderItems[reportingDAL.OrderItems.Count - 1].OneProductTotalPrice = productTable[0].Price * row.Amount;
}

// Accepte Changes
reportingDAL.Orders.AcceptChanges();
reportingDAL.Companys.AcceptChanges();
reportingDAL.OrderItems.AcceptChanges();
reportingDAL.Products.AcceptChanges();
reportDoc.SetDataSource(reportingDAL);
crystalReportViewer1.ReportSource = reportDoc;
}
catch (Exception ex)
{
MessageBox.Show("加载报表出错: " + ex.Message, "加载报表", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void OrderReport_FormClosing(object sender, FormClosingEventArgs e)
{
reportDoc.Dispose();
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。