示例#1
0
        public void refresh(bool Data)
        {
            if (Data) // Nếu vừa sửa xong
            {
                // Lấy lại đối tượng mới
                var db = new MANAGEMENT_STORE_Entities();
                product = db.Detail_Product.SingleOrDefault(x => x.ID_Product == product.ID_Product);

                // Làm mới danh sách ở trang trước (delegate 2 cấp)
                if (RefreshProductList != null)
                {
                    RefreshProductList.Invoke(true);
                }
            }
            // Đưa thông tin lên UI
            txbProductName.Text   = product.NameProduct;
            txbProductId.Text     = product.ID_Product;
            txbOriginalPrice.Text = product.Original_Price.ToString();
            txbSupplier.Text      = manage.GetSupplier(product);
            txbDateImport.Text    = manage.GetDateImport(product).ToString();
            txbCurrAmount.Text    = product.Amount_Current.ToString() + "/" + manage.GetinitialAmount(product).ToString();
            // editProductType.Text = product.ProductType;
            if (product.Description_Pro != null)
            {
                txbDescri.Text = product.Description_Pro;
            }
            if (product.Image_Path != null)
            {
                BitmapImage source = new BitmapImage(new Uri(product.Image_Path));
                imgProduct.Source = source;
            }

            // Lấy tên loại sản phẩm và các hóa đơn liên quan
            Thread thread = new Thread(delegate()
            {
                try
                {
                    var db = new MANAGEMENT_STORE_Entities();
                    string productTypeName             = db.Type_product.Find(product.ID_TypeProduct).Type_Product1; // Lấy tên loại sản phẩm
                    ObservableCollection <Bills> bills = manage.Load_Bill(product.ID_Product);                       // Lấy danh sách hóa đơn

                    // Đưa lên UI
                    Dispatcher.Invoke(() => {
                        txbProductType.Text  = productTypeName;
                        listBill.ItemsSource = bills;
                        // Hiện thông báo nếu không có Bill nào
                        if (bills.Count == 0)
                        {
                            noBillAnnounce.Visibility = Visibility.Visible;
                        }
                    });
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            });

            thread.Start();
        }
        private void BtnExport_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Dialog()
            {
                Message = "Xuất dữ liệu ra tập tin Excel?"
            };

            dialog.Owner = Window.GetWindow(this);
            if (dialog.ShowDialog() == false)
            {
                return;
            }

            // Mở hộp thoại lưu tập tin
            Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
            saveFileDialog.DefaultExt = ".xlsx";
            saveFileDialog.Filter     = "Excel Workbook (.xlsx)|*.xlsx";

            if (false == saveFileDialog.ShowDialog())
            {
                return;
            }
            string filename = saveFileDialog.FileName;

            Workbook  workbook  = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            worksheet.Name = "Product";

            // Ghi các cột
            worksheet.Cells["A1"].PutValue("Product Name");
            worksheet.Cells["B1"].PutValue("Product ID");
            worksheet.Cells["C1"].PutValue("Original Price");
            worksheet.Cells["D1"].PutValue("Date Input");
            worksheet.Cells["E1"].PutValue("Initial Amount");
            worksheet.Cells["F1"].PutValue("Current Amount");
            worksheet.Cells["G1"].PutValue("Capital");
            worksheet.Cells["H1"].PutValue("Description");
            worksheet.Cells["I1"].PutValue("Supplier ID");
            worksheet.Cells["J1"].PutValue("Type ID");
            worksheet.Cells["K1"].PutValue("Image_Path");

            for (int i = 0; i < products.Count; i++)
            {
                worksheet.Cells[$"A{i + 2}"].PutValue(products[i].NameProduct);
                worksheet.Cells[$"B{i + 2}"].PutValue(products[i].ID_Product);
                worksheet.Cells[$"C{i + 2}"].PutValue(products[i].Original_Price);

                DateTime time = dbProduct.GetDateImport(products[i]);
                worksheet.Cells[$"D{i + 2}"].PutValue(time.ToString());
                int s = dbProduct.GetinitialAmount(products[i]);
                worksheet.Cells[$"E{i + 2}"].PutValue(s.ToString());
                worksheet.Cells[$"F{i + 2}"].PutValue(products[i].Amount_Current);

                worksheet.Cells[$"G{i + 2}"].PutValue("awr");
                worksheet.Cells[$"H{i + 2}"].PutValue(products[i].Description_Pro);
                worksheet.Cells[$"I{i + 2}"].PutValue(products[i].ID_Supplier);
                worksheet.Cells[$"J{i + 2}"].PutValue(products[i].ID_TypeProduct);
                worksheet.Cells[$"H{i + 2}"].PutValue(products[i].Image_Path);
            }

            // Lưu lại
            worksheet.AutoFitColumns();
            workbook.Save(filename);
        }