Пример #1
0
        private void metroButtonEdit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Xác nhận chỉnh sửa thông tin?",
                                "Xác nhận xóa", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            HocSinh sv = new HocSinh();

            sv.MaHocSinh  = metroTextBoxID.Text;
            sv.TenHocSinh = metroTextBoxName.Text;
            sv.QueQuan    = (int)metroComboBoxHometown.SelectedValue;
            sv.GioiTinh   = metroRadioButtonMale.Checked;
            sv.NgaySinh   = dateTimePickerBirthday.Value;

            if (DataUlti.SuaThongTinHocSinh(sv))
            {
                MessageBox.Show("Thành công");
                ReloadForm();
            }
            else
            {
                MessageBox.Show("Thất bại");
            }
        }
Пример #2
0
        private void metroButtonFinish_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn hoàn thành bài thi?\r\n" +
                                "Lưu ý: không thể hoàn tác sau khi xác nhận?",
                                "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            timer1.Stop();
            metroLabel2.Visible       = false;
            metroLabel3.Visible       = false;
            status.Visible            = false;
            groupBoxInterface.Visible = false;
            groupBoxInfo.Visible      = false;
            metroButtonExit.Visible   = true;
            metroLabelStat.Top        = 120;
            metroLabelStat.Visible    = true;

            if (offi == true)
            {
                DataUlti.KetThucBaiThi(MHS, MaKyThi);
                ShowResult();
            }
            else
            {
                ShowPracticeResult();
            }
        }
Пример #3
0
        public FormProfile(string id)
        {
            InitializeComponent();
            metroComboBoxHometown.DataSource    = DataUlti.DSQueQuan();
            metroComboBoxHometown.DisplayMember = "TenDiaPhuong";
            metroComboBoxHometown.ValueMember   = "MaVung";

            HocSinh sv = DataUlti.TimHocSinh(id);

            dateTimePickerBirthday.Format       = DateTimePickerFormat.Custom;
            dateTimePickerBirthday.CustomFormat = "dd-MM-yyyy";

            metroTextBoxID.Text   = sv.MaHocSinh;
            metroTextBoxName.Text = sv.TenHocSinh;
            try
            {
                metroComboBoxHometown.SelectedValue = sv.QueQuan;
                dateTimePickerBirthday.Value        = sv.NgaySinh.Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            metroTextBoxClass.Text = sv.Lop;
            metroTextBoxGrade.Text = sv.Khoi.ToString();

            metroRadioButtonMale.Checked   = sv.GioiTinh == true ? true : false;
            metroRadioButtonFemale.Checked = sv.GioiTinh == false ? true : false;
        }
Пример #4
0
        private void FormTest_Load(object sender, EventArgs e)
        {
            status.Text             = "bình thường";
            metroLabelStat.Visible  = false;
            metroButtonExit.Visible = false;
            metroLabelStat.Visible  = false;
            LoadInfo();
            listQuestion = DataUlti.DSCauHoi(MaDeThi);
            listAnswer   = DataUlti.DSDapAn(MaDeThi);

            timer1.Tick += Timer1_Tick;
            timer1.Start();

            answerCount = listAnswer.Count / 4;
            for (int i = 0; i < answerCount; i++)
            {
                metroComboBoxQuestion.Items.Add((i + 1).ToString());
            }

            metroLabelProgress.Text             = string.Format("Đã hoàn thành: {0}/{1}", 0, answerCount);
            metroComboBoxQuestion.SelectedIndex = 0;

            metroLabelHint.Visible  = false;
            metroLabelHint2.Visible = false;

            Reload(test);
            showSpoil = true;
        }
        private void BtnFileOpen_Click(object sender, EventArgs e)
        {
            try
            {
                // Choose what file with the specific ext to open
                fileDialogImportExcel.Filter = "Excel files (*.xlsx)|*.xlsx|Excel files (*.xls)|*.xls";
                DialogResult dialog = fileDialogImportExcel.ShowDialog();

                if (dialog == DialogResult.OK)
                {
                    // load to gridview
                    dgvImportUserList.Columns.Clear();
                    var dt = DataUlti.ReadExcelFile(fileDialogImportExcel.FileName);
                    dgvImportUserList.DataSource         = dt;
                    dgvImportUserList.AllowUserToAddRows = false;

                    // enable update button
                    btnUpdateFromImport.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
Пример #6
0
 private void metroButtonRestore_Click(object sender, EventArgs e)
 {
     try
     {
         listChoices = DataUlti.XemLichSuBaiLam(MHS, MaDeThi, MaKyThi);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         return;
     }
     metroLabelProgress.Text = string.Format("Đã hoàn thành {0}/{1}", listChoices.Count, answerCount);
     MessageBox.Show("Khôi phục thành công");
 }
Пример #7
0
        private void ReloadForm()
        {
            metroComboBoxHometown.DataSource    = null;
            metroComboBoxHometown.DataSource    = DataUlti.DSQueQuan();
            metroComboBoxHometown.DisplayMember = "TenDiaPhuong";
            metroComboBoxHometown.ValueMember   = "MaVung";
            HocSinh sv = DataUlti.TimHocSinh(metroTextBoxID.Text);

            metroTextBoxName.Text               = sv.TenHocSinh;
            dateTimePickerBirthday.Value        = sv.NgaySinh.Value;
            metroComboBoxHometown.SelectedValue = sv.QueQuan;
            metroRadioButtonMale.Checked        = sv.GioiTinh == true ? true : false;
            metroRadioButtonFemale.Checked      = sv.GioiTinh == false ? true : false;
        }
Пример #8
0
        private void BtnXuatFileCauHoi_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
                DialogResult result = saveFileDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    if (File.Exists(saveFileDialog.FileName))
                    {
                        File.Delete(saveFileDialog.FileName);
                    }

                    var DSCauHoi = DataUlti.DSCauHoi();

                    using (DataTable dt = new DataTable())
                    {
                        dt.Columns.Add("MaCauHoi", typeof(string));
                        dt.Columns.Add("Mon", typeof(string));
                        dt.Columns.Add("DoKho", typeof(int));
                        dt.Columns.Add("Khoi", typeof(int));
                        dt.Columns.Add("NoiDung", typeof(string));
                        dt.Columns.Add("GoiY", typeof(string));

                        foreach (var ch in DSCauHoi)
                        {
                            dt.Rows.Add(ch.MaCauHoi, ch.Mon, ch.DoKho, ch.Khoi, ch.NoiDung, ch.GoiY);
                        }

                        if (!DataUlti.WriteExcelFile(saveFileDialog.FileName, dt, 2))
                        {
                            MessageBox.Show("Không thể xuất ra file excel");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Xuất file thành công");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
        private void BtnXuatDSHS_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
                DialogResult result = saveFileDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    if (File.Exists(saveFileDialog.FileName))
                    {
                        File.Delete(saveFileDialog.FileName);
                    }

                    var DSHocSinh = DataUlti.DSHocSinh();

                    using (DataTable dt = new DataTable())
                    {
                        dt.Columns.Add("MaHocSinh", typeof(string));
                        dt.Columns.Add("TenHocSinh", typeof(string));
                        dt.Columns.Add("Khoi", typeof(int));
                        dt.Columns.Add("Lop", typeof(string));
                        dt.Columns.Add("NgaySinh", typeof(string));
                        dt.Columns.Add("QueQuan", typeof(int));
                        dt.Columns.Add("GioiTinh", typeof(int));
                        foreach (var hs in DSHocSinh)
                        {
                            dt.Rows.Add(hs.MaHocSinh, hs.TenHocSinh, hs.Khoi, hs.Lop, hs.NgaySinh, hs.QueQuan, hs.GioiTinh);
                        }

                        if (!DataUlti.WriteExcelFile(saveFileDialog.FileName, dt, 0))
                        {
                            MessageBox.Show("Không thể xuất ra file excel");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Xuất file thành công");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
        private void BtnXuatDSGV_Click(object sender, EventArgs e)
        {
            try
            {
                saveFileDialog.Filter = "Excel files (*.xls)|*.xls";
                DialogResult result = saveFileDialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    if (File.Exists(saveFileDialog.FileName))
                    {
                        File.Delete(saveFileDialog.FileName);
                    }

                    var DSGiaoVien = DataUlti.DSGiaoVien();

                    using (DataTable dt = new DataTable())
                    {
                        dt.Columns.Add("MaGiaoVien", typeof(string));
                        dt.Columns.Add("TenGiaoVien", typeof(string));
                        dt.Columns.Add("NgaySinh", typeof(string));
                        dt.Columns.Add("QueQuan", typeof(int));
                        dt.Columns.Add("GioiTinh", typeof(int));
                        dt.Columns.Add("DayMon", typeof(string));
                        foreach (var gv in DSGiaoVien)
                        {
                            dt.Rows.Add(gv.MaGiaoVien, gv.TenGiaoVien, gv.NgaySinh, gv.QueQuan, gv.GioiTinh, gv.DayMon);
                        }

                        if (!DataUlti.WriteExcelFile(saveFileDialog.FileName, dt, 1))
                        {
                            MessageBox.Show("Không thể xuất ra file excel");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Xuất file thành công");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
Пример #11
0
        private void Timer1_Tick(object sender, EventArgs e)
        {
            SaveProgress();
            c = c - 0.5;
            Int64    temp = (long)c * 10000000;
            TimeSpan ts   = new TimeSpan(temp);

            metroLabelTimer.Text = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
                                                 ts.Hours, ts.Minutes, ts.Seconds);
            // Khi hết giờ thì tự động ngắt tất cả.
            if (c == 0)
            {
                MessageBox.Show("Đã hết giờ làm bài!");
                DataUlti.KetThucBaiThi(MHS, MaKyThi);
                ShowResult();
            }
        }
Пример #12
0
        private void metroButtonSignIn_Click(object sender, EventArgs e)
        {
            TaiKhoan tk = new TaiKhoan()
            {
                ChuTaiKhoan = null,
                TenDangNhap = metroTextBoxUsername.Text,
                MatKhau     = metroTextBoxPassword.Text
            };

            if (DataUlti.KiemTraHopLe(tk))
            {
                if (tk.PhanHe == 1)                 // Học sinh
                {
                    MessageBox.Show(string.Format("Đăng nhập thành công, chào mừng {0}", tk.ChuTaiKhoan));

                    FormMain FM = new FormMain(tk.ChuTaiKhoan);
                    FM.FormClosed += FM_FormClosed;
                    this.Hide();
                    FM.ShowDialog();
                }
                else if (tk.PhanHe == 2)                 // Giáo viên
                {
                    MessageBox.Show(string.Format("Đăng nhập thành công , chào mừng {0}", tk.ChuTaiKhoan));

                    Form1 GV = new Form1();
                    GV.FormClosed += GV_FormClosed;
                    this.Hide();
                    GV.ShowDialog();
                }
                else                 // Admin
                {
                    MessageBox.Show(string.Format("Đăng nhập thành công, chào mừng {0}", tk.ChuTaiKhoan));

                    FormAdmin admin = new FormAdmin();
                    admin.FormClosed += Admin_FormClosed;
                    this.Hide();
                    admin.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Đăng nhập thất bại, mật khẩu hoặc tên tài khoản không đúng", "Lỗi",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void FormMain_Load(object sender, EventArgs e)
        {
            LoadSchedule();
            metroComboBoxSubject.DataSource    = DataUlti.DSMon();
            metroComboBoxSubject.DisplayMember = "TenMonHoc";
            metroComboBoxSubject.ValueMember   = "MaMonHoc";
            metroComboBoxSubject.SelectedIndex = 0;
            metroComboBoxGrade.SelectedIndex   = 0;

            dgvPractice.CellClick += DgvPractice_CellClick;
            dgvSchedule.CellClick += DgvSchedule_CellClick;


            metroComboBoxSubject.SelectedIndexChanged += MetroComboBoxSubject_SelectedIndexChanged;
            metroComboBoxGrade.SelectedIndexChanged   += MetroComboBoxGrade_SelectedIndexChanged;
            LoadPractice();
            LoadResult();
        }
        public void LoadDgvUpdateUser()
        {
            dgvUpdateUser.Rows.Clear();
            dgvUpdateUser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DataGridViewRow  row  = null;
            DataGridViewCell cell = null;


            // get user list
            var list = DataUlti.DSTaiKhoan();

            foreach (var user in list)
            {
                row = new DataGridViewRow();

                row.Tag = user;

                // User ID
                cell       = new DataGridViewTextBoxCell();
                cell.Value = user.ChuTaiKhoan;
                row.Cells.Add(cell);

                // User Account
                cell       = new DataGridViewTextBoxCell();
                cell.Value = user.TenDangNhap;
                row.Cells.Add(cell);

                // Password
                cell       = new DataGridViewTextBoxCell();
                cell.Value = user.MatKhau;
                row.Cells.Add(cell);


                // User Type
                cell       = new DataGridViewTextBoxCell();
                cell.Value = user.PhanHe == 1 ? "Học sinh" : user.PhanHe == 2 ? "Giáo viên" : "Admin";
                row.Cells.Add(cell);

                // Add to Data Grid View
                dgvUpdateUser.Rows.Add(row);
            }
        }
Пример #15
0
        private int SaveProgress()
        {
            if (getAnswerID() == "")
            {
                return(-1); // Chưa chọn gì hết thì không lưu.
            }
            LuuTru temp = listChoices.Where(s => s.MaCauHoi == listQuestion[Test].MaCauHoi).SingleOrDefault();

            if (temp == null)
            {
                listChoices.Add(new LuuTru(MaKyThi, MaDeThi, MHS, listQuestion[Test].MaCauHoi, getAnswerID()));
            }
            else
            {
                temp.MaDapAn = getAnswerID();
            }
            IEnumerable <LuuTru> t = listChoices.OrderBy(f => f.MaCauHoi);

            listChoices             = t.ToList();
            metroLabelProgress.Text = string.Format("Đã hoàn thành {0}/{1}", t.Count(), answerCount);

            if (offi)
            {
                if (DataUlti.LuuDapAn(listChoices))
                {
                    status.Text = "Lưu tự động thành công";
                    return(1);
                }
                else
                {
                    status.Text = "Lưu tự động thất bại";
                    return(0);
                }
            }
            else
            {
                return(-1);
            }
        }
Пример #16
0
        public FormTest(string id, string MDT, string MKT, bool Official, int TG)
        {
            InitializeComponent();
            MHS            = id;
            MaDeThi        = MDT;
            MaKyThi        = MKT;
            offi           = true;
            ThoiGianLamBai = TG;

            // Thời gian bắt đầu.
            TimeSpan TGBD = DataUlti.LayThoiGianBD(MHS, MKT);
            TimeSpan ts   = DateTime.Now.TimeOfDay;
            double   n1   = TGBD.Subtract(ts).TotalSeconds;
            double   n2   = TG * 60;

            // Thời gian còn lại.
            c = n1 + n2;

            metroButtonHint.Visible  = false;
            metroButtonSpoil.Visible = false;
            this.Load += FormTest_Load;
        }
Пример #17
0
        public FormTest()
        {
            InitializeComponent();
            MHS     = "1612030009";
            MaDeThi = "MH01120001";
            MaKyThi = "CK201801MH01";

            metroLabelStat.Visible  = false;
            metroButtonExit.Visible = false;
            metroLabelStat.Visible  = false;

            listQuestion = DataUlti.DSCauHoi(MaDeThi);
            listAnswer   = DataUlti.DSDapAn(MaDeThi);

            answerCount = listAnswer.Count / 4;
            for (int i = 0; i < answerCount; i++)
            {
                metroComboBoxQuestion.Items.Add((i + 1).ToString());
            }

            metroLabelProgress.Text             = string.Format("Đã hoàn thành: {0}/{1}", 0, answerCount);
            metroComboBoxQuestion.SelectedIndex = 0;
            Reload(test);
        }
        private void BtnUpdateUser_Click(object sender, EventArgs e)
        {
            if (dgvUpdateUser.SelectedRows.Count > 0)
            {
                // get selected row
                var tk = dgvUpdateUser.SelectedRows[0].Tag as TaiKhoan;

                // get new data
                //tk.ChuTaiKhoan = lblAccount.Text;
                //tk.TenDangNhap = txtUserName.Text;
                tk.MatKhau = txtMatKhau.Text;
                tk.PhanHe  = cbUserType.SelectedIndex + 1;

                if (DataUlti.SuaThongTinTaiKhoan(tk))
                {
                    MessageBox.Show("Đã cập nhật thành công");
                    LoadDgvUpdateUser();
                }
                else
                {
                    MessageBox.Show("Không thể cập nhật");
                }
            }
        }
        private void DgvSchedule_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != dgvSchedule.Columns["war"].Index)
            {
                return;
            }

            DateTime NgayThi = DateTime.Parse(dgvSchedule.SelectedRows[0].Cells[2].Value.ToString());
            string   MKT     = dgvSchedule.SelectedRows[0].Cells[0].Value.ToString();

            // Lấy tgian quy định.
            ThoiGianLamBai = (int)dgvSchedule.SelectedRows[0].Cells[4].Value;

            //MessageBox.Show(t.CompareTo(DateTime.Now).ToString());

            if (dgvSchedule.SelectedRows[0].Cells[6].Value.ToString() == "Đóng" ||
                (NgayThi.Date.CompareTo(DateTime.Now.Date) != 0))
            {
                MessageBox.Show("Đã hết hạn làm bài thi");
                return;
            }
            else
            {
                TimeSpan BD  = DataUlti.LayThoiGianBD(MHS, MKT);
                TimeSpan now = DateTime.Now.TimeOfDay;
                if (BD != null && BD.Subtract(now).TotalMinutes > ThoiGianLamBai)
                {
                    MessageBox.Show("Đã hết hạn làm bài thi");
                    return;
                }
            }



            using (var ds = new QuanLyThiTracNghiemDataContext())
            {
                try
                {
                    var q = ds.DSDUTHIs.Where(d => d.MaHocSinh == MHS && d.MaKyThi == MKT).SingleOrDefault();
                    if (q != null)
                    {
                        if (q.DaThi == true)
                        {
                            MessageBox.Show("Bài thi đã được hoàn thành");
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }



            //MessageBox.Show(MDT);
            if (MessageBox.Show("Sẵn sàng làm bài thi?",
                                "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            string MDT = null;

            // Kiểm tra xem có đang làm bài dang dở.
            if (DataUlti.TiepTucThi(MHS, MKT) == false)
            {
                // Bốc đề ngẫu nhiên trong kỳ thi.
                var    q        = DataUlti.LayDeThi(MKT);
                Random roulette = new Random();
                MDT = q[roulette.Next(0, q.Count)].MaDeThi;

                // Bắt đầu làm bài:
                DataUlti.BatDauThi(MHS, MKT, MDT);
            }
            else
            {
                // Kiểm tra tgian làm bài:
                TimeSpan now     = DateTime.Now.TimeOfDay;
                TimeSpan lastLog = DataUlti.LayThoiGianBD(MHS, MKT);
                if (now.Subtract(lastLog).TotalMinutes > ThoiGianLamBai)
                {
                    MessageBox.Show("Đã hết hạn làm bài");
                    return;
                }

                // Tiếp tục.
                MDT = DataUlti.DSThi(MKT)
                      .Where(d => d.MaHocSinh == MHS && d.MaKyThi == MKT)
                      .Select(d => d.MaDeThi).SingleOrDefault().ToString();
            }
            FormTest FT = new FormTest(MHS, MDT, MKT, true, ThoiGianLamBai);

            FT.ShowDialog();

            LoadResult();
        }
        private void LoadSchedule()
        {
            dgvSchedule.AutoGenerateColumns = false;

            IList <KyThi> t = DataUlti.LayLichThi(MHS);

            dgvSchedule.DataSource = (from k in t
                                      from m in DataUlti.DSMon()
                                      where m.MaMonHoc == k.MonThi
                                      select new
            {
                k.MaKyThi,
                k.TenKyThi,
                k.NgayThi,
                k.ThoiGianLamBai,
                ThiThu = k.ThiThu == true ? "Thi thử" : "Chính thức",
                m.TenMonHoc,
                KetThuc = k.KetThuc == true ? "Đóng" : "Mở",
                war = "Thi"
            }).ToList();

            dgvSchedule.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvSchedule.RowHeadersVisible   = false;
            dgvSchedule.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn dgvCol = new DataGridViewTextBoxColumn();

            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dgvCol.HeaderText       = "Mã kỳ thi     ";
            dgvCol.DataPropertyName = "MaKyThi";
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Tên kỳ thi";
            dgvCol.DataPropertyName = "TenKyThi";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.Fill;
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Ngày thi";
            dgvCol.DataPropertyName = "NgayThi";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Môn Thi";
            dgvCol.DataPropertyName = "TenMonHoc";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Thời gian";
            dgvCol.DataPropertyName = "ThoiGianLamBai";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Hình thức";
            dgvCol.DataPropertyName = "ThiThu";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvSchedule.Columns.Add(dgvCol);

            dgvCol                  = new DataGridViewTextBoxColumn();
            dgvCol.HeaderText       = "Trạng thái";
            dgvCol.DataPropertyName = "KetThuc";
            dgvCol.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvSchedule.Columns.Add(dgvCol);

            DataGridViewButtonColumn dgvBut = new DataGridViewButtonColumn();

            dgvBut.HeaderText       = "Thi          ";
            dgvBut.Name             = "war";
            dgvBut.AutoSizeMode     = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvBut.DataPropertyName = "war";

            dgvSchedule.Columns.Add(dgvBut);
        }
Пример #21
0
        private void BtnNhapFileCauHoi_Click(object sender, EventArgs e)
        {
            try
            {
                fileDialogImportExcel.Filter = "Excel files (*.xlsx)|*.xlsx|Excel files (*.xls)|*.xls";
                DialogResult dialog = fileDialogImportExcel.ShowDialog();

                if (dialog == DialogResult.OK)
                {
                    var notExistList = new List <DataRow>();
                    var existList    = new List <DataRow>();

                    // Lấy dữ liệu từ file excel
                    //DataGridView tmpDGV = new DataGridView();
                    //tmpDGV.Columns.Clear();
                    var dt = DataUlti.ReadExcelFile(fileDialogImportExcel.FileName);
                    //tmpDGV.DataSource = dt;

                    // Lấy danh sách câu hỏi
                    var chList = DataUlti.DSCauHoi();

                    //Lấy ra danh sách các câu hỏi chưa có trong database
                    IEnumerable <string> list = chList.Select(ch => ch.MaCauHoi);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        var MaCH = dt.Rows[i]["MaCauHoi"].ToString();
                        if (!list.Contains(MaCH))
                        {
                            notExistList.Add(dt.Rows[i]);
                        }
                        else
                        {
                            existList.Add(dt.Rows[i]);
                        }
                    }

                    // add vào database
                    // tạo đối tương CauHoi
                    if (notExistList.Count > 0)
                    {
                        int flag = 0;
                        foreach (var item in notExistList)
                        {
                            var ch = new CauHoi()
                            {
                                MaCauHoi = item["MaCauHoi"].ToString(),
                                Mon      = item["Mon"].ToString(),
                                DoKho    = int.Parse(item["DoKho"].ToString()),
                                Khoi     = int.Parse(item["Khoi"].ToString()),
                                NoiDung  = item["NoiDung"].ToString(),
                                GoiY     = item["GoiY"].ToString()
                            };

                            if (!DataUlti.ThemCauHoi(ch))
                            {
                                flag = 1;
                                break;
                            }
                        }

                        if (flag == 0)
                        {
                            dt.Columns.Clear();
                            dt.Rows.Clear();
                            MessageBox.Show("Đã thêm vào CSDL");
                            LoadCauHoi();
                        }
                        else
                        {
                            MessageBox.Show("Có lỗi xãy ra");
                        }
                    }
                    else
                    {
                        if (existList.Count > 0)
                        {
                            string str = "";
                            if (existList.Count < 5)
                            {
                                foreach (var item in existList)
                                {
                                    str += item["MaCauHoi"].ToString() + "\n";
                                }
                                MessageBox.Show(string.Format("Các câu hỏi bị trùng:\n{0}", str));
                            }
                            else
                            {
                                MessageBox.Show(string.Format("Số lượng câu hỏi đã tồn tại quá nhiều: {0}", existList.Count));
                            }
                        }
                    }
                    notExistList.Clear();
                    existList.Clear();
                    dt.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void BtnUpdateFromImport_Click(object sender, EventArgs e)
        {
            var notExistList = new List <DataGridViewCellCollection>();
            var existList    = new List <DataGridViewCellCollection>();

            if (dgvImportUserList.RowCount > 0)
            {
                var firstCellValue = dgvImportUserList.Rows[0].Cells[0].Value.ToString();
                if (firstCellValue.Contains("GV"))
                {
                    // Get list of giaovien
                    var gvList = DataUlti.DSGiaoVien();

                    try
                    {
                        //Lấy ra danh sách các giáo viên chưa có trong database
                        IEnumerable <string> list = gvList.Select(gv => gv.MaGiaoVien);
                        for (int i = 0; i < dgvImportUserList.RowCount; i++)
                        {
                            var MaGV = dgvImportUserList.Rows[i].Cells["MaGiaoVien"].Value;
                            if (!list.Contains(MaGV))
                            {
                                notExistList.Add(dgvImportUserList.Rows[i].Cells);
                            }
                            else
                            {
                                existList.Add(dgvImportUserList.Rows[i].Cells);
                            }
                        }

                        // add vào database
                        // tạo đối tương GiaoVien
                        if (notExistList.Count > 0)
                        {
                            int flag = 0;
                            foreach (var item in notExistList)
                            {
                                var gv = new GiaoVien()
                                {
                                    MaGiaoVien  = item["MaGiaoVien"].Value.ToString(),
                                    TenGiaoVien = item["TenGiaoVien"].Value.ToString(),
                                    NgaySinh    = DateTime.Parse(item["NgaySinh"].Value.ToString()),
                                    QueQuan     = int.Parse(item["QueQuan"].Value.ToString()),
                                    //GioiTinh = bool.Parse(item["GioiTinh"].Value.ToString()),
                                    GioiTinh = (item["GioiTinh"].Value.ToString() == "0") ? false : true,
                                    DayMon   = item["DayMon"].Value.ToString()
                                };

                                var ctk = new ChuTaiKhoan()
                                {
                                    ID = item["MaGiaoVien"].Value.ToString()
                                };

                                if (!DataUlti.ThemChuTaiKhoan(ctk) && !DataUlti.ThemGiaoVien(gv))
                                {
                                    flag = 1;
                                    break;
                                }
                            }

                            if (flag == 0)
                            {
                                dgvImportUserList.Columns.Clear();
                                MessageBox.Show("Đã thêm vào CSDL");
                                btnUpdateFromImport.Visible = false;
                                LoadDgvUpdateUser();
                            }
                            else
                            {
                                MessageBox.Show("Có lỗi xãy ra");
                            }
                        }
                        else
                        {
                            if (existList.Count > 0)
                            {
                                string str = "";
                                if (existList.Count < 5)
                                {
                                    foreach (var item in existList)
                                    {
                                        str += item["MaGiaoVien"].Value.ToString() + "\n";
                                    }
                                    MessageBox.Show(string.Format("Các giáo viên bị trùng:\n{0}", str));
                                }
                                else
                                {
                                    MessageBox.Show(string.Format("Số lượng giáo viên đã tồn tại quá nhiều: {0}", existList.Count));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        existList.Clear();
                        notExistList.Clear();
                        return;
                    }
                }
                else
                {
                    var hsList = DataUlti.DSHocSinh();

                    try
                    {
                        //Lấy ra danh sách các học sinh chưa có trong database
                        IEnumerable <string> list = hsList.Select(hs => hs.MaHocSinh);
                        for (int i = 0; i < dgvImportUserList.RowCount; i++)
                        {
                            var MaHS = dgvImportUserList.Rows[i].Cells["MaHocSinh"].Value;
                            if (!list.Contains(MaHS))
                            {
                                notExistList.Add(dgvImportUserList.Rows[i].Cells);
                            }
                            else
                            {
                                existList.Add(dgvImportUserList.Rows[i].Cells);
                            }
                        }

                        // add vào database
                        // tạo đối tương HocSinh
                        if (notExistList.Count > 0)
                        {
                            int flag = 0;
                            foreach (var item in notExistList)
                            {
                                var hs = new HocSinh()
                                {
                                    MaHocSinh  = item["MaHocSinh"].Value.ToString(),
                                    TenHocSinh = item["TenHocSinh"].Value.ToString(),
                                    Khoi       = int.Parse(item["Khoi"].Value.ToString()),
                                    Lop        = item["Lop"].Value.ToString(),
                                    NgaySinh   = DateTime.Parse(item["NgaySinh"].Value.ToString()),
                                    QueQuan    = int.Parse(item["QueQuan"].Value.ToString()),
                                    //GioiTinh = bool.Parse(item["GioiTinh"].Value.ToString())
                                    GioiTinh = (item["GioiTinh"].Value.ToString() == "0") ? false : true
                                };

                                var ctk = new ChuTaiKhoan()
                                {
                                    ID = item["MaHocSinh"].Value.ToString()
                                };

                                if (!DataUlti.ThemChuTaiKhoan(ctk) && !DataUlti.ThemHocSinh(hs))
                                {
                                    flag = 1;
                                    break;
                                }
                            }

                            if (flag == 0)
                            {
                                dgvImportUserList.Columns.Clear();
                                MessageBox.Show("Đã thêm vào CSDL");
                                btnUpdateFromImport.Visible = false;
                                LoadDgvUpdateUser();
                            }
                            else
                            {
                                MessageBox.Show("Có lỗi xãy ra");
                            }
                        }
                        else
                        {
                            if (existList.Count > 0)
                            {
                                string str = "";
                                if (existList.Count < 5)
                                {
                                    foreach (var item in existList)
                                    {
                                        str += item["MaHocSinh"].Value.ToString() + "\n";
                                    }
                                    MessageBox.Show(string.Format("Các học sinh bị trùng:\n{0}", str));
                                }
                                else
                                {
                                    MessageBox.Show(string.Format("Số lượng học sinh đã tồn tại quá nhiều: {0}", existList.Count));
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        existList.Clear();
                        notExistList.Clear();
                        return;
                    }
                }
            }
            existList.Clear();
            notExistList.Clear();
        }