crystalReportViewer+ReceiveCrystalReport.rpt+DataSet.xsd做报表

原文地址:http://blog.csdn.net/happy09li/article/details/6931959


crystalReportViewer(报表查看器)+ReceiveCrystalReport.rpt(报表文件)+DataSet.xsd(数据集)

1.crystalReportViewer 的ReportSource属性指定一个ReceiveCrystalReport.rpt文件
2.添加一个dataset.xsd文件,添加报表要显示的字段信息(一定要与数据库中一致)
3.打开ReceiveCrystalReport.rpt文件,在右边窗口DatabaseFields右键(Database EXpert)

  

crystalReportViewer+ReceiveCrystalReport.rpt+DataSet.xsd做报表_第1张图片


在左边窗口打开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.横打,左右双打(一页打双份)


crystalReportViewer+ReceiveCrystalReport.rpt+DataSet.xsd做报表_第2张图片


把宽度设置为页面的一半即可,最好是比页面的一半还要少点




------------------------------------------------------------------不预览直接选择打印机打印-----------------------------------------------------

                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();
                rpt.SetDataSource(LoadDetailInfo(orderItem).Tables["orderItem"]);
                this.crystalReportViewer1.ReportSource = rpt;
            }
            catch (Exception ex)
            {

            }
   
        }

        //数据源
        private DataSet LoadDetailInfo(List<receiveShipmentOrderItem> orderItem)
        {
            DataSet ds = CreateReportDataSet();
            if (orderItem != null)
            {
                DataTable deliveryorderTable = ds.Tables["orderItem"];
                foreach (receiveShipmentOrderItem item in orderItem)
                {
                    DataRow dr = deliveryorderTable.NewRow();
                    dr["id"] = item.purchaseProduct.id;
                    dr["orderCode"] = item.purchaseProduct.orderCode;
                    dr["receiveShipmentQty"] = item.receiveShipmentQty.ToString();
                    dr["reimburseMark"] = (item.purchaseProduct.reimburseMark ? "是" : "否"); 
                    dr["image"] = item.purchaseProduct.image;//二进制类型
                    deliveryorderTable.Rows.Add(dr);
                }
            }
            return ds;
        }

        private DataSet CreateReportDataSet()
        {
            DataSet ds = new DataSet();
            DataTable deliveryorderTable = new DataTable("orderItem");
            deliveryorderTable.Columns.Add(new DataColumn("Image",System.Type.GetType("System.Byte[]"))));        
            deliveryorderTable.Columns.Add(new DataColumn("orderCode"));
            deliveryorderTable.Columns.Add(new DataColumn("receiveShipmentQty"));
            deliveryorderTable.Columns.Add(new DataColumn("reimburseMark"));
            ds.Tables.Add(deliveryorderTable);

            return ds;
        }


//设置头参数

        private ParameterFields LoadOrderInfo(receiveShipmentOrder orderview)
        {
            ParameterFields orderInfo = new ParameterFields();
            if (orderview != null)
            {
                orderInfo.Add(GetParameterField("supplierName", orderview.supplier.name));
                orderInfo.Add(GetParameterField("name", orderview.createUser.name));
                string state="";
                orderInfo.Add(GetParameterField("state", state));
                orderInfo.Add(GetParameterField("receiveCode", orderview.code));
            }
            return orderInfo;
        }

        //设置参数字段
        private ParameterField GetParameterField(string name, string value)
        {
            ParameterField field = new ParameterField();
            ParameterDiscreteValue fieldValue = new ParameterDiscreteValue();
            field.ParameterFieldName = name;
            fieldValue.Value = value;
            field.CurrentValues.Add(fieldValue);
            return field;
        }

 

 

 

------------------------------------------------------------------主副报表关联显示------------------------------------------------------------------

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


你可能感兴趣的:(crystalReportViewer+ReceiveCrystalReport.rpt+DataSet.xsd做报表)