Exemple #1
0
        public static void LoadData()
        {
            List <SubjectRecord> subjectRecords = new List <SubjectRecord>();

            // Open csv to get subjects
            using (var reader = new StringReader(Properties.Resources.ScheduleOfUndergraduateUnits))
                using (var csv = new CsvReader(reader))
                {
                    var record  = new SubjectRecord();
                    var records = csv.EnumerateRecords(record);
                    foreach (var r in records)
                    {
                        var subjectRecord = (SubjectRecord)r.Clone();
                        subjectRecord.InitialSetup();
                        // Make sure the subject is being offered
                        if (subjectRecord.Actual.Semesters.Any())
                        {
                            // subject codes should be unique - if this code already exists then replace it
                            bool found = false;
                            for (int i = 0; i < subjectRecords.Count; i++)
                            {
                                if (subjectRecords[i].Code == subjectRecord.Code)
                                {
                                    found             = true;
                                    subjectRecords[i] = subjectRecord;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                subjectRecords.Add(subjectRecord);
                            }
                            Debug.Assert(subjectRecords.Count(s => s.Code == subjectRecord.Code) == 1, "the code should appear exactly once");
                        }
                    }
                }

            foreach (SubjectRecord subject in subjectRecords)
            {
                MasterList.AddSubject(subject.Actual);
            }

            Debug.WriteLine("loaded subjects");

            StringBuilder descriptionBuilder = new StringBuilder();
        private void btnSave_Click(object sender, EventArgs e)
        {
            List <SubjectRecord> insert    = new List <SubjectRecord>();
            List <string>        existName = new List <string>();
            bool pass = true;

            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (row.IsNewRow)
                {
                    continue;
                }

                row.Cells[colName.Index].ErrorText  = "";
                row.Cells[colType.Index].ErrorText  = "";
                row.Cells[colGroup.Index].ErrorText = "";

                string name  = row.Cells[colName.Index].Value + "";
                string type  = row.Cells[colType.Index].Value + "";
                string group = row.Cells[colGroup.Index].Value + "";

                if (string.IsNullOrWhiteSpace(name))
                {
                    row.Cells[colName.Index].ErrorText = "科目名稱不可為空白";
                    pass = false;
                }
                else
                {
                    if (existName.Contains(name))
                    {
                        row.Cells[colName.Index].ErrorText = "科目名稱不可重複";
                        pass = false;
                    }
                    else
                    {
                        existName.Add(name);
                    }
                }

                if (string.IsNullOrWhiteSpace(group))
                {
                    row.Cells[colGroup.Index].ErrorText = "群組名稱不可為空白";
                    pass = false;
                }
                else if (!_groupList.Contains(group))
                {
                    row.Cells[colGroup.Index].ErrorText = "群組名稱有誤,請選擇預設群組";
                    pass = false;
                }

                if (string.IsNullOrWhiteSpace(type))
                {
                    row.Cells[colType.Index].ErrorText = "組別不可為空白";
                    pass = false;
                }

                row.ErrorText = "";
                List <string> errors = new List <string>();
                if (!string.IsNullOrWhiteSpace(row.Cells[colGroup.Index].ErrorText))
                {
                    errors.Add(row.Cells[colGroup.Index].ErrorText);
                }

                if (!string.IsNullOrWhiteSpace(row.Cells[colType.Index].ErrorText))
                {
                    errors.Add(row.Cells[colType.Index].ErrorText);
                }

                row.ErrorText = string.Join(",", errors);

                SubjectRecord record = new SubjectRecord();
                record.Name = row.Cells[colName.Index].Value + "";
                //record.EnglishName = row.Cells[colEnName.Index].Value + "";
                record.ChineseName = row.Cells[colChName.Index].Value + "";
                record.Group       = row.Cells[colGroup.Index].Value + "";
                record.Type        = row.Cells[colType.Index].Value + "";
                insert.Add(record);
            }

            if (pass)
            {
                List <SubjectRecord> allRecord = _A.Select <SubjectRecord>();
                _A.DeletedValues(allRecord);

                if (insert.Count > 0)
                {
                    _A.InsertValues(insert);
                }

                //EventHandler eh = FISCA.InteractionService.PublishEvent("SubjectChange");
                //eh(null, EventArgs.Empty);

                this.Close();
            }
            else
            {
                MessageBox.Show("資料有誤,請確認後再儲存");
            }
        }