public void ArrTongHangXuat()
        {
            ArrBCXH = new Entities.BCXuatHangTheoTungKho[khohang.Length];
            for (int i = 0; i < khohang.Length; i++)
            {
                LayHDBanHang_TheoKho(khohang[i].MaKho);
                LayPhieuDieuChuyenKhoNoiBo_TheoKho(khohang[i].MaKho);
                LayPhieuXuatHuy_TheoMaKho(khohang[i].MaKho);
                LayTraLaiNCC_TheoMaKho(khohang[i].MaKho);
                LayChiTiet_HangHoaXuat_TheoKho(khohang[i].MaKho, khohang[i].TenKho);
                LayChiTiet_SoLuong_HangHoaXuat_TheoKho();

                ArrBCXH[i] = new Entities.BCXuatHangTheoTungKho();

                int TongHangHoaXuatTheoKho = 0;
                for (int j = 0; j < ctBCXH.Length; j++)
                {
                    TongHangHoaXuatTheoKho = TongHangHoaXuatTheoKho + ctBCXH[j].TongSoLuongXuat;
                }
                ArrBCXH[i].MaKho = khohang[i].MaKho;
                ArrBCXH[i].TenKho = khohang[i].TenKho;
                ArrBCXH[i].TongSoLuongXuat = TongHangHoaXuatTheoKho;
                ctBCXH = null;
            }
        }
        private void frmBCXuatHangTheoTungKho_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < cbbthang.Items.Count; i++)
            {
                if (cbbthang.Items[i].ToString() == datesv.Month.ToString())
                {
                    cbbthang.SelectedIndex = i;
                    break;
                }
            }

            for (int i = 0; i < cbbnam.Items.Count; i++)
            {
                if (cbbnam.Items[i].ToString() == datesv.Year.ToString())
                {
                    cbbnam.SelectedIndex = i;
                    break;
                }
            }
            LayNgay();
            LayKhoHang();
            LayHangHoa();
            SelectGoiHang();
            LayHDBanHang();
            LayChiTiet_HDBanHang();
            LayPhieuDieuChuyenKhoNoiBo();
            LayChiTiet_PhieuDieuChuyenKho();
            LayPhieuXuatHuy();
            LayChiTiet_XuatHuy();
            LayTraLaiNCC();
            LayChiTiet_TraLaiNCC();
            LayChiTiet_HangHoaXuat();

            ArrTongHangXuat();
            ///////////////////////////////MRK FIX
            List<Entities.BCXuatHangTheoTungKho> tem0 = new List<BCXuatHangTheoTungKho>();
            double tong0 = 0;
            foreach (Entities.BCXuatHangTheoTungKho item in ArrBCXH)
            {
                tong0 += item.TongSoLuongXuat;
                tem0.Add(item);
            }
            Entities.BCXuatHangTheoTungKho tem1 = new BCXuatHangTheoTungKho();
            tem1.TenKho = "Tổng: ";
            tem1.TongSoLuongXuat = (int)tong0;
            tem0.Add(tem1);
            //////////////////////////////////////
            //dtgvhienthi.DataSource = ArrBCXH;
            dtgvhienthi.DataSource = tem0.ToArray();
            FixDatagridview();
            dtgvhienthi.Focus();
            ctBCXH_Search = ArrBCXH;
            if (ArrBCXH.Length > 0)
            {
                tsslchitiet.Enabled = true;
                tsslPdf.Enabled = true;
                tsslExcel.Enabled = true;
                tsslWord.Enabled = true;
            }
            else
            {
                tsslchitiet.Enabled = false;
                tsslPdf.Enabled = false;
                tsslExcel.Enabled = false;
                tsslWord.Enabled = false;
            }
        }
        private void btnhienthi_Click(object sender, EventArgs e)
        {
            if (KiemTra())
            {
                LayNgay();
                LayKhoHang();
                LayHangHoa();
                SelectGoiHang();
                LayHDBanHang();
                LayChiTiet_HDBanHang();
                LayPhieuDieuChuyenKhoNoiBo();
                LayChiTiet_PhieuDieuChuyenKho();
                LayPhieuXuatHuy();
                LayChiTiet_XuatHuy();
                LayTraLaiNCC();
                LayChiTiet_TraLaiNCC();
                LayChiTiet_HangHoaXuat();

                ArrTongHangXuat();
                ///////////////////////////////MRK FIX
                List<Entities.BCXuatHangTheoTungKho> tem0 = new List<BCXuatHangTheoTungKho>();
                double tong0 = 0;
                foreach (Entities.BCXuatHangTheoTungKho item in ArrBCXH)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    tong0 += item.TongSoLuongXuat;
                    tem0.Add(item);
                }
                Entities.BCXuatHangTheoTungKho tem1 = new BCXuatHangTheoTungKho();
                tem1.TenKho = "Tổng: ";
                tem1.TongSoLuongXuat = (int)tong0;
                tem0.Add(tem1);
                //////////////////////////////////////
                //dtgvhienthi.DataSource = ArrBCXH;
                dtgvhienthi.DataSource = tem0.ToArray();
                FixDatagridview();
                dtgvhienthi.Focus();
                ctBCXH_Search = ArrBCXH;
                if (ArrBCXH.Length > 0)
                {
                    tsslchitiet.Enabled = true;
                    tsslPdf.Enabled = true;
                    tsslExcel.Enabled = true;
                    tsslWord.Enabled = true;
                }
                else
                {
                    tsslchitiet.Enabled = false;
                    tsslPdf.Enabled = false;
                    tsslExcel.Enabled = false;
                    tsslWord.Enabled = false;
                }
            }
        }
        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (rdbTen.Checked == true)
            {

                if (ctBCXH_Search == null)
                {
                    BCXuatHangTheoTungKho[] tkkt = new BCXuatHangTheoTungKho[0];
                    dtgvhienthi.DataSource = tkkt;
                    FixDatagridview();

                    tsslchitiet.Enabled = false;
                    tsslExcel.Enabled = false;
                    tsslPdf.Enabled = false;
                    tsslWord.Enabled = false;
                }
                else
                    if (ctBCXH_Search != null)
                    {
                        if (txtTimKiem.Text.Length == 0)
                        {
                            BCXuatHangTheoTungKho[] tkkt = new BCXuatHangTheoTungKho[0];
                            dtgvhienthi.DataSource = tkkt;
                            FixDatagridview();

                            tsslchitiet.Enabled = false;
                            tsslExcel.Enabled = false;
                            tsslPdf.Enabled = false;
                            tsslWord.Enabled = false;
                        }
                        else
                        {
                            int ctBCXH_Search_count = 0;

                            for (int i = 0; i < ctBCXH_Search.Length; i++)
                            {
                                int index = -1;
                                index = ctBCXH_Search[i].TenKho.ToLower().IndexOf(txtTimKiem.Text.Trim().ToLower());
                                if (index >= 0)
                                {
                                    ctBCXH_Search_count++;
                                }
                            }
                            BCXuatHangTheoTungKho[] ctBCXH_Search1 = new BCXuatHangTheoTungKho[ctBCXH_Search_count];
                            ctBCXH_Search_count = 0;

                            for (int i = 0; i < ctBCXH_Search.Length; i++)
                            {
                                int index = -1;
                                index = ctBCXH_Search[i].TenKho.ToLower().IndexOf(txtTimKiem.Text.Trim().ToLower());
                                if (index >= 0)
                                {
                                    ctBCXH_Search1[ctBCXH_Search_count] = ctBCXH_Search[i];
                                    ctBCXH_Search_count++;
                                }
                            }
                            if (ctBCXH_Search_count == 0)
                            {
                                tsslchitiet.Enabled = false;
                                tsslExcel.Enabled = false;
                                tsslPdf.Enabled = false;
                                tsslWord.Enabled = false;
                            }
                            else
                            {
                                tsslchitiet.Enabled = true;
                                tsslExcel.Enabled = true;
                                tsslPdf.Enabled = true;
                                tsslWord.Enabled = true;
                            }
                            ///////////////////////////////MRK FIX
                            List<Entities.BCXuatHangTheoTungKho> tem0 = new List<BCXuatHangTheoTungKho>();
                            double tong0 = 0;
                            foreach (Entities.BCXuatHangTheoTungKho item in ctBCXH_Search1)
                            {
                                tong0 += item.TongSoLuongXuat;
                                tem0.Add(item);
                            }
                            Entities.BCXuatHangTheoTungKho tem1 = new BCXuatHangTheoTungKho();
                            tem1.TenKho = "Tổng: ";
                            tem1.TongSoLuongXuat = (int)tong0;
                            tem0.Add(tem1);
                            //////////////////////////////////////
                            //dtgvhienthi.DataSource = ctBCXH_Search1;
                            dtgvhienthi.DataSource = tem0.ToArray();
                            FixDatagridview();
                        }
                    }
            }
            else
                if (rdbMa.Checked == true)
                {
                    if (ctBCXH_Search == null)
                    {
                        BCXuatHangTheoTungKho[] tkkt = new BCXuatHangTheoTungKho[0];
                        dtgvhienthi.DataSource = tkkt;
                        FixDatagridview();

                        tsslchitiet.Enabled = false;
                        tsslExcel.Enabled = false;
                        tsslPdf.Enabled = false;
                        tsslWord.Enabled = false;

                    }
                    else
                        if (ctBCXH_Search != null)
                        {
                            if (txtTimKiem.Text.Length == 0)
                            {
                                BCXuatHangTheoTungKho[] tkkt = new BCXuatHangTheoTungKho[0];
                                dtgvhienthi.DataSource = tkkt;
                                FixDatagridview();

                                tsslchitiet.Enabled = false;
                                tsslExcel.Enabled = false;
                                tsslPdf.Enabled = false;
                                tsslWord.Enabled = false;

                            }
                            else
                            {
                                int ctBCXH_Search_count = 0;

                                for (int i = 0; i < ctBCXH_Search.Length; i++)
                                {
                                    int index = -1;
                                    index = ctBCXH_Search[i].MaKho.ToLower().IndexOf(txtTimKiem.Text.Trim().ToLower());
                                    if (index >= 0)
                                    {
                                        ctBCXH_Search_count++;
                                    }
                                }
                                BCXuatHangTheoTungKho[] ctBCXH_Search1 = new BCXuatHangTheoTungKho[ctBCXH_Search_count];
                                ctBCXH_Search_count = 0;

                                for (int i = 0; i < ctBCXH_Search.Length; i++)
                                {
                                    int index = -1;
                                    index = ctBCXH_Search[i].MaKho.ToLower().IndexOf(txtTimKiem.Text.Trim().ToLower());
                                    if (index >= 0)
                                    {
                                        ctBCXH_Search1[ctBCXH_Search_count] = ctBCXH_Search[i];
                                        ctBCXH_Search_count++;
                                    }
                                }
                                if (ctBCXH_Search_count == 0)
                                {
                                    tsslchitiet.Enabled = false;
                                    tsslExcel.Enabled = false;
                                    tsslPdf.Enabled = false;
                                    tsslWord.Enabled = false;
                                }
                                else
                                {
                                    tsslchitiet.Enabled = true;
                                    tsslExcel.Enabled = true;
                                    tsslPdf.Enabled = true;
                                    tsslWord.Enabled = true;
                                }
                                ///////////////////////////////MRK FIX
                                List<Entities.BCXuatHangTheoTungKho> tem0 = new List<BCXuatHangTheoTungKho>();
                                double tong0 = 0;
                                foreach (Entities.BCXuatHangTheoTungKho item in ctBCXH_Search1)
                                {
                                    tong0 += item.TongSoLuongXuat;
                                    tem0.Add(item);
                                }
                                Entities.BCXuatHangTheoTungKho tem1 = new BCXuatHangTheoTungKho();
                                tem1.TenKho = "Tổng: ";
                                tem1.TongSoLuongXuat = (int)tong0;
                                tem0.Add(tem1);
                                //////////////////////////////////////
                                //dtgvhienthi.DataSource = ctBCXH_Search1;
                                dtgvhienthi.DataSource = tem0.ToArray();
                                FixDatagridview();
                            }
                        }
                }
        }
 private void rdbTatCa_CheckedChanged(object sender, EventArgs e)
 {
     if (rdbTatCa.Checked == true)
     {
         txtTimKiem.Text = "";
         if (ctBCXH_Search.Length > 0)
         {
             tsslchitiet.Enabled = true;
             tsslPdf.Enabled = true;
             tsslExcel.Enabled = true;
             tsslWord.Enabled = true;
         }
         else
         {
             tsslchitiet.Enabled = false;
             tsslPdf.Enabled = false;
             tsslExcel.Enabled = false;
             tsslWord.Enabled = false;
         }
         ///////////////////////////////MRK FIX
         List<Entities.BCXuatHangTheoTungKho> tem0 = new List<BCXuatHangTheoTungKho>();
         double tong0 = 0;
         foreach (Entities.BCXuatHangTheoTungKho item in ctBCXH_Search)
         {
             tong0 += item.TongSoLuongXuat;
             tem0.Add(item);
         }
         Entities.BCXuatHangTheoTungKho tem1 = new BCXuatHangTheoTungKho();
         tem1.TenKho = "Tổng: ";
         tem1.TongSoLuongXuat = (int)tong0;
         tem0.Add(tem1);
         //////////////////////////////////////
         //dtgvhienthi.DataSource = ctBCXH_Search;
         dtgvhienthi.DataSource = tem0.ToArray();
         FixDatagridview();
     }
 }