示例#1
0
        void CapNhatDuLieuGridView()
        {
            int i_TGConLai = 0;

            if (SqlConn == null || SqlConn.State != ConnectionState.Open)
            {
                Database.Open(ref this.SqlConn);
            }
            if (SqlConn.State == ConnectionState.Open)
            {
                try
                {
                    //Cập nhật thông số
                    TienIch.tshienthi ts = new tshienthi();
                    ts.LoadAll(SqlConn);
                    s_Header               = ts.Header;
                    s_Footer               = ts.Footer;
                    i_SoDongHienThi        = ts.SoDongHienThi;
                    iThoiGianHienThiDuLieu = ts.TGHienThi;
                    //
                    TienIch.DsKhachHang dskhachhang = new DsKhachHang();
                    dt_KhachHang = dskhachhang.LoadDSKHChoSuaChua(SqlConn);
                    if (bHienThiDuLieuMoi) //định kỳ 5s hiển thị dữ liệu
                    {
                        if (dt_KhachHang != null && dt_KhachHang.Rows.Count > 0)
                        {
                            DataTable dt_dsKH = new DataTable();
                            if (dt_KhachHang.Rows.Count < i_SoDongHienThi)
                            {
                                dt_dsKH           = dt_KhachHang.Copy();
                                iDsKhachHangIndex = 0;
                            }
                            else
                            {
                                dt_dsKH = dt_KhachHang.Clone();
                                if (iDsKhachHangIndex >= dt_KhachHang.Rows.Count)
                                {
                                    iDsKhachHangIndex = 0;
                                }
                                for (int i = 0; i < i_SoDongHienThi; i++)
                                {
                                    if ((iDsKhachHangIndex + i) < dt_KhachHang.Rows.Count)
                                    {
                                        dt_dsKH.ImportRow(dt_KhachHang.Rows[iDsKhachHangIndex + i]);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                iDsKhachHangIndex += i_SoDongHienThi;
                                if (iDsKhachHangIndex >= dt_KhachHang.Rows.Count)
                                {
                                    iDsKhachHangIndex = 0;
                                }
                            }
                            grdKhachHang.DataSource = dt_dsKH;
                        }
                        else
                        {
                            grdKhachHang.DataSource = dt_KhachHang;
                            iDsKhachHangIndex       = 0;
                        }
                    }
                    ///////////////////////////////////////////////
                    TienIch.DsBanNang dsbannang = new DsBanNang();
                    dt_BanNang = dsbannang.LoadAll(SqlConn);
                    try
                    {
                        if (dt_BanNang != null && dt_BanNang.Rows.Count > 0)
                        {
                            dt_BanNang.Columns.Add("thoigianconlai", typeof(decimal)).DefaultValue = 0;
                            dt_BanNang.Columns.Add("trangthaibn", typeof(string)).DefaultValue     = "";
                            foreach (DataRow r in dt_BanNang.Rows)
                            {
                                TrangThai.TrangThaiBanNang TT_TrangThai = (TrangThai.TrangThaiBanNang)Convert.ToInt16(r["trangthai"].ToString());
                                r["trangthaibn"] = TienIch.TrangThai.TrangThaiBanNang2String((TrangThai.TrangThaiBanNang)Convert.ToInt16(r["trangthai"].ToString()));
                                DateTime TGCapNhat = r["thoigianbd"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(r["thoigianbd"]);
                                TimeSpan diff      = DateTime.Now.Subtract(TGCapNhat);
                                Int32    minutes   = Convert.ToInt32(Math.Ceiling(diff.TotalMinutes)); //Làm tròn lên để hiển thị khớp với bảng LED
                                if (r["tongthoigian"] == DBNull.Value)
                                {
                                    r["tongthoigian"] = 0;
                                }
                                i_TGConLai          = Convert.ToInt32(minutes) >= Convert.ToInt32(r["tongthoigian"]) ? 0 : Convert.ToInt32(r["tongthoigian"]) - Convert.ToInt32(minutes);
                                r["thoigianconlai"] = i_TGConLai;
                                if (TT_TrangThai != TrangThai.TrangThaiBanNang.DangSuaChua)
                                {
                                    r["hoten"]          = "";
                                    r["thoigianbd"]     = DateTime.MinValue;
                                    r["biensoxe"]       = "";
                                    r["thoigianconlai"] = -1;
                                    r["tongthoigian"]   = -1;
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                    if (bHienThiDuLieuMoi) //định kỳ 5s hiển thị dữ liệu
                    {
                        if (dt_BanNang != null && dt_BanNang.Rows.Count > 0)
                        {
                            DataTable dt_HienThi = new DataTable();
                            if (dt_BanNang.Rows.Count < i_SoDongHienThi)
                            {
                                dt_HienThi      = dt_BanNang.Copy();
                                iDsBanNangIndex = 0;
                            }
                            else
                            {
                                dt_HienThi = dt_BanNang.Clone();
                                if (iDsBanNangIndex >= dt_BanNang.Rows.Count)
                                {
                                    iDsBanNangIndex = 0;
                                }
                                for (int i = 0; i < i_SoDongHienThi; i++)
                                {
                                    if ((iDsBanNangIndex + i) < dt_BanNang.Rows.Count)
                                    {
                                        dt_HienThi.ImportRow(dt_BanNang.Rows[iDsBanNangIndex + i]);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                iDsBanNangIndex += i_SoDongHienThi;
                                if (iDsBanNangIndex >= dt_BanNang.Rows.Count)
                                {
                                    iDsBanNangIndex = 0;
                                }
                            }
                            grdBanNang.DataSource = dt_HienThi;
                        }
                        else
                        {
                            grdBanNang.DataSource = dt_BanNang;
                            iDsBanNangIndex       = 0;
                        }
                    }
                    //////////////////////////////////////////////////////////////////////////////////////
                    DsTraXe ds_TraXe = new DsTraXe();
                    // Hiển thị lên grid
                    dt_TraXe = ds_TraXe.LoadDsChuaLayXe(SqlConn);
                    if (bHienThiDuLieuMoi) //định kỳ 5s hiển thị dữ liệu
                    {
                        if (dt_TraXe != null && dt_TraXe.Rows.Count > 0)
                        {
                            DataTable dt_HienThi = new DataTable();
                            if (dt_TraXe.Rows.Count < i_SoDongHienThi)
                            {
                                dt_HienThi    = dt_TraXe.Copy();
                                iDsTraXeIndex = 0;
                            }
                            else
                            {
                                dt_HienThi = dt_TraXe.Clone();
                                if (iDsTraXeIndex >= dt_TraXe.Rows.Count)
                                {
                                    iDsTraXeIndex = 0;
                                }
                                for (int i = 0; i < i_SoDongHienThi; i++)
                                {
                                    if ((iDsTraXeIndex + i) < dt_TraXe.Rows.Count)
                                    {
                                        dt_HienThi.ImportRow(dt_TraXe.Rows[iDsTraXeIndex + i]);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                iDsTraXeIndex += i_SoDongHienThi;
                                if (iDsTraXeIndex >= dt_TraXe.Rows.Count)
                                {
                                    iDsTraXeIndex = 0;
                                }
                            }
                            grdTraXe.DataSource = dt_HienThi;
                        }
                        else
                        {
                            grdTraXe.DataSource = dt_TraXe;
                            iDsTraXeIndex       = 0;
                        }
                    }
                    // Chạy chữ footer
                    DataTable dt_KQTraXe = ds_TraXe.LoadDsHienThiThongBao(SqlConn);

                    if (dt_KQTraXe != null && dt_KQTraXe.Rows.Count > 0)
                    {
                        if (dt_ThongBaoLayXe == null)
                        {
                            dt_ThongBaoLayXe = dt_KQTraXe.Clone();// Clone() only clones the table structure. It does not also clone the data.
                        }
                        for (int i = 0; i < dt_KQTraXe.Rows.Count; i++)
                        {
                            bool isDupe = false;
                            for (int j = 0; j < dt_ThongBaoLayXe.Rows.Count; j++)
                            {
                                if (dt_KQTraXe.Rows[i][0].ToString() == dt_ThongBaoLayXe.Rows[j][0].ToString())
                                {
                                    isDupe = true;
                                    break;
                                }
                            }

                            if (!isDupe)
                            {
                                dt_ThongBaoLayXe.ImportRow(dt_KQTraXe.Rows[i]);
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
        void CapNhatDuLieuGridView()
        {
            int    i_TongBanNang           = 0;
            int    i_BanNangBan            = 0;
            int    i_TGConLai              = 0;
            int    i_BanNang_SelectIndex   = 0;
            int    i_KhachHang_SelectIndex = 0;
            int    i_TraXe_SelectIndex     = 0;
            string s_BanNangSuaChuaXong    = "";

            if (SqlConn == null || SqlConn.State != ConnectionState.Open)
            {
                Database.Open(ref this.SqlConn);
            }
            if (SqlConn.State == ConnectionState.Open)
            {
                try
                {
                    i_BanNang_SelectIndex   = grvBanNang.FocusedRowHandle;
                    i_KhachHang_SelectIndex = grvKhachHang.FocusedRowHandle;
                    i_TraXe_SelectIndex     = grvTraXe.FocusedRowHandle;

                    TienIch.DsKhachHang dskhachhang = new DsKhachHang();
                    DataTable           dt_DsKH     = dskhachhang.LoadDSKHChoSuaChua(SqlConn);
                    grdKhachHang.DataSource = dt_DsKH;

                    TienIch.DsBanNang dsbannang = new DsBanNang();
                    DataTable         dt_DsBN   = dsbannang.LoadTheoTrangThaiKichHoat(SqlConn, TrangThai.TrangThaiKichHoatBanNang.HoatDong);

                    TienIch.DsTraXe ds_TraXe = new DsTraXe();
                    DataTable       dt_TraXe = ds_TraXe.LoadDsChuaLayXe(SqlConn);
                    grdTraXe.DataSource = dt_TraXe;

                    try
                    {
                        if (dt_DsBN != null && dt_DsBN.Rows.Count > 0)
                        {
                            dt_DsBN.Columns.Add("thoigianconlai", typeof(decimal)).DefaultValue = 0;
                            dt_DsBN.Columns.Add("trangthaibn", typeof(string)).DefaultValue     = "";
                            i_TongBanNang = dt_DsBN.Rows.Count;
                            foreach (DataRow r in dt_DsBN.Rows)
                            {
                                TrangThai.TrangThaiBanNang TT_TrangThai = (TrangThai.TrangThaiBanNang)Convert.ToInt16(r["trangthai"].ToString());
                                r["trangthaibn"] = TienIch.TrangThai.TrangThaiBanNang2String((TrangThai.TrangThaiBanNang)Convert.ToInt16(r["trangthai"].ToString()));
                                DateTime TGCapNhat = r["thoigianbd"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(r["thoigianbd"]);
                                TimeSpan diff      = DateTime.Now.Subtract(TGCapNhat);
                                //double minutes = diff.TotalMinutes;
                                Int32 minutes = Convert.ToInt32(Math.Ceiling(diff.TotalMinutes)); //Làm tròn lên để hiển thị khớp với bảng LED
                                if (r["tongthoigian"] == DBNull.Value)
                                {
                                    r["tongthoigian"] = 0;
                                }
                                i_TGConLai          = Convert.ToInt32(minutes) >= Convert.ToInt32(r["tongthoigian"]) ? 0 : Convert.ToInt32(r["tongthoigian"]) - Convert.ToInt32(minutes);
                                r["thoigianconlai"] = i_TGConLai;
                                if (TT_TrangThai != TrangThai.TrangThaiBanNang.DangSuaChua)
                                {
                                    r["hoten"]          = "";
                                    r["thoigianbd"]     = DateTime.MinValue;
                                    r["biensoxe"]       = "";
                                    r["thoigianconlai"] = -1;
                                    r["tongthoigian"]   = -1;
                                }
                                if (i_TGConLai <= 0 && TT_TrangThai == TrangThai.TrangThaiBanNang.DangSuaChua)
                                {
                                    s_BanNangSuaChuaXong += String.Format("[{0}]", r["maql"].ToString());
                                }
                            }
                        }
                        lblSLBanNang.Caption  = String.Format("Hoạt động: {0}/{1}", i_BanNangBan, i_TongBanNang);
                        grdBanNang.DataSource = dt_DsBN;

                        grvBanNang.FocusedRowHandle   = i_BanNang_SelectIndex;
                        grvKhachHang.FocusedRowHandle = i_KhachHang_SelectIndex;
                        grvTraXe.FocusedRowHandle     = i_TraXe_SelectIndex;
                    }
                    catch
                    {
                    }
                }
                catch
                {
                }
                finally
                {
                    if (s_BanNangSuaChuaXong != s_NhacNhoTraXe)
                    {
                        s_NhacNhoTraXe = s_BanNangSuaChuaXong;
                        if (s_NhacNhoTraXe != "")
                        {
                            ntfNhacNho.Visible         = true;
                            ntfNhacNho.Icon            = SystemIcons.Exclamation;
                            ntfNhacNho.BalloonTipTitle = "IKY-LED";
                            ntfNhacNho.BalloonTipText  = "Bàn nâng " + s_NhacNhoTraXe + " đã sửa chữa xong";
                            ntfNhacNho.BalloonTipIcon  = ToolTipIcon.Warning;
                            ntfNhacNho.ShowBalloonTip(10000);
                        }
                    }
                }
            }
        }