crystalReportViewer(报表查看器)+ReceiveCrystalReport.rpt(报表文件)+DataSet.xsd(数据集)
1.crystalReportViewer 的ReportSource属性指定一个ReceiveCrystalReport.rpt文件
2.添加一个dataset.xsd文件,添加报表要显示的字段信息(一定要与数据库中一致)
3.打开ReceiveCrystalReport.rpt文件,在右边窗口DatabaseFields右键(Database EXpert)
在左边窗口打开ProjectData>ADO.NET Datasets 选择一个DataSet,添加到右边,点确定,
再将其字段拖入打开的报表文件中即可
private void PrintReceiveOrder_Load(object sender, EventArgs e)
{
try
{
ReceiveCrystalReport rpt = new ReceiveCrystalReport();
DataSet ds = new DataSet();
string strConn = "Data Source=192.168.1.1;Initial Catalog=Test;User ID=sa;Password=123456";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("Select id,pay_time,type,remark from test", conn);
da.Fill(ds, "test");
rpt.SetDataSource(ds.Tables["test"]);
this.crystalReportViewer1.ReportSource = rpt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
1.固定条数,调整节高或是写公式:
在detail节按右键,Format section...
选择 New Page after,然后点击右边的公式按钮,输入:
RecordNumber mod 5= 0
则会每五条记录翻页。
2.换行显示
选中有一个字段 右键 Fromat Object 勾选 Can Grow 即可
3设置主副表报关联显示
选择子报表
点右键 选择 change subreport links |
4.纸张横打
右键->打印设计->
5.横打,左右双打(一页打双份)
把宽度设置为页面的一半即可,最好是比页面的一半还要少点
------------------------------------------------------------------不预览直接选择打印机打印-----------------------------------------------------
PrintDialog print = new PrintDialog();
print.ShowDialog();//选择打印机
PrintUPSCrystalReport ups = new PrintUPSCrystalReport();//报表设计
ups.SetParameterValue("Code", "PH20120327");//设置参数
ups.SetParameterValue("Telephone", "123456792");
ups.PrintOptions.PrinterName = print.PrinterSettings.PrinterName;//用选择的打印机进行打印,不选择则用默认打印机打印
ups.PrintToPrinter(1, true, 1, 1);//关键这句,不用预览,直接打印
ups.SetDataSource(dt);//设置数据源
---------------------------------------------------------------------------用对象做做报表数据-----------------------------------------
public PrintReceiveOrder(receiveShipmentOrder orderInfo,List<receiveShipmentOrderItem> orderItem)
{
InitializeComponent();
BindReport(orderInfo, orderItem);
}
//绑定报表
private void BindReport(receiveShipmentOrder orderInfo, List<receiveShipmentOrderItem> orderItem)
{
ParameterFields parameterFields = LoadOrderInfo(orderInfo);
try
{
this.crystalReportViewer1.ParameterFieldInfo = parameterFields;
ReceiveCrystalReport rpt = new ReceiveCrystalReport();
DataSet ds = new DataSet();
ds.Tables.Add(getInoData());
ds.Tables.Add(getItemsData());
rpt.SetDataSource(ds);
this.crystalReportViewer1.ReportSource = rpt;
}
catch (Exception ex)
{
}
}
///报表头参数,InStockOrderInfo---DataSet.xsd 中的表
private ReportDs.InStockOrderInfoDataTable getInoData()------------------------------------------------------------------主副报表关联显示------------------------------------------------------------------
private void bindList(List<WaitDeliveryView> deliveryList)
{
OrderCrystalReport rpt = new OrderCrystalReport();
rpt.SetDataSource(LoadDetailInfo(deliveryList).Tables["packageItem"]);
rpt.Subreports["OrderDetailReport.rpt"].SetDataSource(LoadDetailInfo(deliveryList).Tables["ProductItem"]);
this.crystalReportViewer1.ReportSource = rpt;
}
//数据源
private DataSet LoadDetailInfo(List<WaitDeliveryView> deliveryList)
{
DataSet ds = CreateReportDataSet();
if (deliveryList != null)
{
DataTable packageTable = ds.Tables["packageItem"];
DataTable ProductTable = ds.Tables["ProductItem"];
int total = 0;
int i = 0;
foreach (WaitDeliveryView item in deliveryList)
{
DataRow dr = packageTable.NewRow();
dr["Id"] = item.Package.id;
dr["OrderCode"] = item.Package.code;
dr["CustomName"] = item.Package.order.buyer.fullName;
dr["DeliveryWay"] = item.Package.delivery.name;
dr["Total"] = total;
packageTable.Rows.Add(dr);
foreach (orderProduct items in item.Package.orderProducts)
{
DataRow drs = ProductTable.NewRow();
drs["Id"] =item.Package.id;
drs["No"] = i + 1;
//显示商品编号 + (SKU)
drs["Item"] = items.code + " " + items.SKU;
drs["Qty"] = items.productQty;
total += items.productQty;
ProductTable.Rows.Add(drs);
}
}
}
return ds;
}
private DataSet CreateReportDataSet()
{
DataSet ds = new DataSet();
DataTable packageTable = new DataTable("packageItem");
packageTable.Columns.Add(new DataColumn("Id"));
packageTable.Columns.Add(new DataColumn("OrderCode"));
packageTable.Columns.Add(new DataColumn("CustomName"));
packageTable.Columns.Add(new DataColumn("DeliveryWay"));
packageTable.Columns.Add(new DataColumn("Total"));
ds.Tables.Add(packageTable);
DataTable ProductTable = new DataTable("ProductItem");
ProductTable.Columns.Add(new DataColumn("Id"));
ProductTable.Columns.Add(new DataColumn("No"));
ProductTable.Columns.Add(new DataColumn("Item"));
ProductTable.Columns.Add(new DataColumn("Qty"));
ds.Tables.Add(ProductTable);
return ds;
}
报表详细相关设置
资料
资料2
资料,,全