[DEVEXPRESS] How to make Label Report in c# winform

Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn source code, tạo báo cáo report in tem nhãn dùng quản lý thông tin thiết bị, tài sản của công ty hoặc cá nhân.

[DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform

Dưới đây là hình ảnh demo ứng dụng C#:

[DEVEXPRESS] How to make Label Report in c# winform

Source code C#:

1, Code FormMain.cs
using DevExpress.Export;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using report;

namespace VNSoftware.frmDongThung
{
    public partial class TaoMaVachcs : DevExpress.XtraEditors.XtraForm
    {
        private DataTable yourDataTable;



        public TaoMaVachcs()
        {
            InitializeComponent();
            InitializeDataTable();
            InitDataDemo();
            dtgv.DataSource = yourDataTable;
        }

        private void InitializeDataTable()
        {           
            yourDataTable = new DataTable();
            yourDataTable.Columns.Add("MA HANG", typeof(string));
            yourDataTable.Columns.Add("VERSION", typeof(string));
            yourDataTable.Columns.Add("SO LUONG/ THUNG", typeof(string));
            yourDataTable.Columns.Add("Ma Cty", typeof(string));
            yourDataTable.Columns.Add("MA VUNG", typeof(string));
            yourDataTable.Columns.Add("LOT DH", typeof(string));
            yourDataTable.Columns.Add("SO TT THUNG", typeof(string));
            yourDataTable.Columns.Add("Ma trong", typeof(string));
            yourDataTable.Columns.Add("QRCODE", typeof(string));
        }
        public int GetRandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max + 1);
        }
        private void InitDataDemo()
        {
            try
            {
                string mahang = "SP240116-01";
                mahang = mahang.PadRight(22);
                string ver = "0";
                ver = ver.PadRight(12);
                string soluong = GetRandomNumber(1, 1500).ToString();
                soluong = soluong.PadRight(14);
                string macty = "LAPTRINHVB2015";
                macty = macty.PadRight(16);
                string mavung = "VBNET";
                string lot = "123456";
                string matrong ="0";
                int j = 0;
                int k = 100;


              
                for (int i = j; i <= k; i++)
                {
                   string sothutu = i.ToString("0000");
                  string qrcode = $"{mahang}{ver}{soluong}{macty}{mavung}{lot}{sothutu}{matrong}";
                    DataRow newRow = yourDataTable.NewRow();
                    newRow["MA HANG"] = mahang;
                    newRow["VERSION"] = ver;
                    newRow["SO LUONG/ THUNG"] = soluong.Trim();
                    newRow["Ma Cty"] = macty;
                    newRow["MA VUNG"] = mavung;
                    newRow["LOT DH"] = lot;
                    newRow["SO TT THUNG"] = sothutu;
                    newRow["Ma trong"] = matrong;
                    newRow["QRCODE"] = qrcode;                
                    yourDataTable.Rows.Add(newRow);
                }           
                gridView1.BestFitColumns();
            }
            catch (FormatException)
            {
                MessageBox.Show("Không thể chuyển đổi chuỗi sang số nguyên.");
            }

        }

        private void btnInNhan_Click(object sender, EventArgs e)
        {
            if (yourDataTable.Rows.Count <= 0)
            {
                XtraMessageBox.Show("Chưa có dữ liệu để in nhãn");
                return;
            }
            var loaiReport = 1;
            var formPrintPreview = new FrmPrintPreview(loaiReport, yourDataTable);
            formPrintPreview.Show();
        }
    }
}

2. Source code form FrmPrintPreview.cs

amespace report
{
    public partial class FrmPrintPreview : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public int _loaiReport;
        public object _dataTable;
        public FrmPrintPreview(int loaiReport, object dataTable)
        {
            InitializeComponent();
            this._loaiReport = loaiReport;
            this._dataTable = dataTable;

        

        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
           
            switch (_loaiReport)
            {
                case 1:
                    {
                        Text = @"IN TEM NHÃN LABEL";
                        var rpt = new reportLabel1();
                        documentViewer1.PrintingSystem = rpt.PrintingSystem;
                        rpt.DataSource = _dataTable;
                        rpt.BindData();
                        rpt.CreateDocument();
                    }
                    break;
            }
        }
    }
}

3. Source code reportLabel1.cs

namespace report
{
    public partial class reportLabel1 : DevExpress.XtraReports.UI.XtraReport
    {
        public reportLabel1()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            txtMaHang.DataBindings.Add("Text", DataSource, "MA HANG");
            txtSoThuTu.DataBindings.Add("Text", DataSource, "SO TT THUNG");
            txtVersion.DataBindings.Add("Text", DataSource, "VERSION");
            txtSoluong.DataBindings.Add("Text", DataSource, "SO LUONG/ THUNG").FormatString = "{0:#,###.##} Pcs";

            txtLotDH.DataBindings.Add("Text", DataSource, "LOT DH");
            txtMaVung.DataBindings.Add("Text", DataSource, "MA VUNG");
            xrBarCode1.DataBindings.Add("Text", DataSource, "QRCODE");
        }
    }
}

Chúc các bạn thành công với thủ thuật trên, để tích hợp để phù hợp với công việc của các bạn nhé

DOWNLOAD SOURCE CODE


PASSWORD UNZIP : HUNG.PRO.VN

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

1 comment:

Hung Program VN said...

Mọi người kiểm tra và áp dụng cho csharp nhé, mình thấy cũng hay LD

All Right Reserved © 2015 By Hung Pro VN