NEWS

[DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#

[DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
Đăng bởi: Thảo Meo - Lượt xem: 2480 14:06:40, 22/04/2024DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn source code các in nhiều hóa đơn cùng một lúc trong Xtrareport Devexpress C#.

[DEVEPXRESS] How to print multi invoice in Xtrareport C#

Đầu tiên, các bạn thiết kế giao diện Report như hình bên dưới:

print_invoice_report

Kết quả khi chạy ứng dụng:

invoice_report_devexpress

Ở bài viết này mình demo 2 loại: List Object và Dataset.

Fullsource code c#:

using DevExpress.XtraPrinting.Preview;
using DevExpress.XtraPrintingLinks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.Links;

using DevExpress.XtraReports.UI;

namespace PrintInvoice
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public List<Invoice> GetSampleData()
        {
            List<Invoice> invoices = new List<Invoice>
    {
        new Invoice
        {
            InvoiceNo = 1001,
            InvoiceDate = new DateTime(2023, 4, 15),
            CustomerName = "Nguyễn Thảo",
            TotalAmount = 1250.75m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product A", Quantity = 2, UnitPrice = 100.50m, TotalPrice = 201.00m },
                new InvoiceItem { ItemName = "Product B", Quantity = 5, UnitPrice = 75.25m, TotalPrice = 376.25m },
                new InvoiceItem { ItemName = "Product C", Quantity = 3, UnitPrice = 224.50m, TotalPrice = 673.50m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1002,
            InvoiceDate = new DateTime(2023, 4, 18),
            CustomerName = "Cái Trí Minh",
            TotalAmount = 875.40m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product D", Quantity = 1, UnitPrice = 375.60m, TotalPrice = 375.60m },
                new InvoiceItem { ItemName = "Product E", Quantity = 4, UnitPrice = 124.95m, TotalPrice = 499.80m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1003,
            InvoiceDate = new DateTime(2023, 4, 20),
            CustomerName = "Võ Sơn Băng",
            TotalAmount = 1500.00m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product F", Quantity = 3, UnitPrice = 200.00m, TotalPrice = 600.00m },
                new InvoiceItem { ItemName = "Product G", Quantity = 2, UnitPrice = 150.75m, TotalPrice = 301.50m },
                new InvoiceItem { ItemName = "Product H", Quantity = 1, UnitPrice = 500.25m, TotalPrice = 500.25m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1004,
            InvoiceDate = new DateTime(2023, 4, 22),
            CustomerName = "Nguyễn Đình Tuyên",
            TotalAmount = 990.50m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product I", Quantity = 2, UnitPrice = 110.25m, TotalPrice = 220.50m },
                new InvoiceItem { ItemName = "Product J", Quantity = 3, UnitPrice = 75.00m, TotalPrice = 225.00m },
                new InvoiceItem { ItemName = "Product K", Quantity = 4, UnitPrice = 140.00m, TotalPrice = 560.00m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1005,
            InvoiceDate = new DateTime(2023, 4, 25),
            CustomerName = "Hoàng Thị Thảo",
            TotalAmount = 675.80m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product L", Quantity = 1, UnitPrice = 375.80m, TotalPrice = 375.80m },
                new InvoiceItem { ItemName = "Product M", Quantity = 2, UnitPrice = 150.00m, TotalPrice = 300.00m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1006,
            InvoiceDate = new DateTime(2023, 4, 28),
            CustomerName = "Nguyễn Phương Nhi",
            TotalAmount = 1150.25m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product N", Quantity = 3, UnitPrice = 200.25m, TotalPrice = 600.75m },
                new InvoiceItem { ItemName = "Product O", Quantity = 2, UnitPrice = 175.50m, TotalPrice = 351.00m },
                new InvoiceItem { ItemName = "Product P", Quantity = 1, UnitPrice = 275.00m, TotalPrice = 275.00m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1007,
            InvoiceDate = new DateTime(2023, 5, 1),
            CustomerName = "Trịnh Quốc Khang",
            TotalAmount = 1450.60m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product Q", Quantity = 4, UnitPrice = 150.50m, TotalPrice = 602.00m },
                new InvoiceItem { ItemName = "Product R", Quantity = 2, UnitPrice = 175.75m, TotalPrice = 351.50m },
                new InvoiceItem { ItemName = "Product S", Quantity = 1, UnitPrice = 500.10m, TotalPrice = 500.10m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1008,
            InvoiceDate = new DateTime(2023, 5, 4),
            CustomerName = "Đinh Minh Thành",
            TotalAmount = 970.25m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product T", Quantity = 1, UnitPrice = 450.25m, TotalPrice = 450.25m },
                new InvoiceItem { ItemName = "Product U", Quantity = 3, UnitPrice = 140.00m, TotalPrice = 420.00m },
                new InvoiceItem { ItemName = "Product V", Quantity = 2, UnitPrice = 100.00m, TotalPrice = 200.00m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1009,
            InvoiceDate = new DateTime(2023, 5, 7),
            CustomerName = "Nguyễn Đình Tuyên",
            TotalAmount = 825.90m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product W", Quantity = 2, UnitPrice = 200.25m, TotalPrice = 400.50m },
                new InvoiceItem { ItemName = "Product X", Quantity = 1, UnitPrice = 425.50m, TotalPrice = 425.50m }
            }
        },
        new Invoice
        {
            InvoiceNo = 1010,
            InvoiceDate = new DateTime(2023, 5, 10),
            CustomerName = "Hoàng Dược Sư",
            TotalAmount = 550.40m,
            InvoiceItems = new List<InvoiceItem>
            {
                new InvoiceItem { ItemName = "Product Y", Quantity = 3, UnitPrice = 100.00m, TotalPrice = 300.00m },
                new InvoiceItem { ItemName = "Product Z", Quantity = 2, UnitPrice = 75.20m, TotalPrice = 150.40m }
            }
        }
    };

            return invoices;
        }


        public DataSet GetSampleDataSet()
        {
            DataSet dataSet = new DataSet();

            DataTable masterTable = new DataTable("Invoice");
            masterTable.Columns.Add("InvoiceNo", typeof(int));
            masterTable.Columns.Add("InvoiceDate", typeof(DateTime));
            masterTable.Columns.Add("CustomerName", typeof(string));
            masterTable.Columns.Add("TotalAmount", typeof(decimal));
            dataSet.Tables.Add(masterTable);

            DataTable detailTable = new DataTable("InvoiceItem");
            detailTable.Columns.Add("InvoiceNo", typeof(int));
            detailTable.Columns.Add("ItemName", typeof(string));
            detailTable.Columns.Add("Quantity", typeof(int));
            detailTable.Columns.Add("UnitPrice", typeof(decimal));
            detailTable.Columns.Add("TotalPrice", typeof(decimal));
            dataSet.Tables.Add(detailTable);

            dataSet.Relations.Add("Invoice_InvoiceItem", masterTable.Columns["InvoiceNo"], detailTable.Columns["InvoiceNo"]);

            List<Invoice> invoices = GetSampleData();
            foreach (Invoice invoice in invoices)
            {
                DataRow masterRow = masterTable.NewRow();
                masterRow["InvoiceNo"] = invoice.InvoiceNo;
                masterRow["InvoiceDate"] = invoice.InvoiceDate;
                masterRow["CustomerName"] = invoice.CustomerName;
                masterRow["TotalAmount"] = invoice.TotalAmount;
                masterTable.Rows.Add(masterRow);

                foreach (InvoiceItem item in invoice.InvoiceItems)
                {
                    DataRow detailRow = detailTable.NewRow();
                    detailRow["InvoiceNo"] = invoice.InvoiceNo;
                    detailRow["ItemName"] = item.ItemName;
                    detailRow["Quantity"] = item.Quantity;
                    detailRow["UnitPrice"] = item.UnitPrice;
                    detailRow["TotalPrice"] = item.TotalPrice;
                    detailTable.Rows.Add(detailRow);
                }
            }

            return dataSet;
        }


        private void simpleButton1_Click(object sender, EventArgs e)
        {

            var invoices = GetSampleData();

            var report = new rptInvoice();

            report.DataSource = invoices;

            var printTool = new DevExpress.XtraReports.UI.ReportPrintTool(report);

            printTool.ShowPreviewDialog();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            var invoices = GetSampleDataSet();

            var report = new rptInvoiceDataSet();

            report.DataSource = invoices;

            var printTool = new DevExpress.XtraReports.UI.ReportPrintTool(report);

            printTool.ShowPreviewDialog();
        }
    }



    public class Invoice
    {
        public int InvoiceNo { get; set; }
        public DateTime InvoiceDate { get; set; }
        public string CustomerName { get; set; }
        public decimal TotalAmount { get; set; }
        public List<InvoiceItem> InvoiceItems { get; set; }
    }

    public class InvoiceItem
    {
        public string ItemName { get; set; }
        public int Quantity { get; set; }
        public decimal UnitPrice { get; set; }
        public decimal TotalPrice { get; set; }
    }
}

Thanks for watching!

Bạn nào thao tác không thành công, có thể liên hệ mình thông tin bên dưới phần tác giả.

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
Đăng bởi: Thảo Meo - Lượt xem: 2480 14:06:40, 22/04/2024DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.