public void QuickAdd(SuggestSubjectCount sData)
        {
            UDTSubjectDef newData = new UDTSubjectDef();
            newData.SchoolYear = _SchoolYear;
            newData.Semester = _Semester;
            newData.Month = _Month;
            newData.Credit = sData.Credit;
            newData.SubjectName = sData.SubjectName;
            newData.SubjecLevel = sData.Level;
            newData.DeptName = sData.DeptName;

            if (CheckCanAddData(newData))
            {
                int rowIdx = dgData.Rows.Add();
                dgData.Rows[rowIdx].Cells[colCredit.Index].Value = newData.Credit;
                dgData.Rows[rowIdx].Cells[colDept.Index].Value = newData.DeptName;
                dgData.Rows[rowIdx].Cells[colSubjectName.Index].Value = newData.SubjectName;
                if (newData.SubjecLevel.HasValue)
                    dgData.Rows[rowIdx].Cells[colSubjectLevel.Index].Value = newData.SubjecLevel.Value;
                dgData.Rows[rowIdx].Tag = newData;
                ReloadRowCount();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                #region Save

                btnSave.Enabled = false;

                // 檢查必填
                foreach (DataGridViewRow dr in dgData.Rows)
                {
                    if (dr.IsNewRow)
                        continue;
                    foreach (DataGridViewCell cell in dr.Cells)
                    {
                        // 級別、科目類別、課表
                        if (cell.ColumnIndex == colSubjectLevel.Index || cell.ColumnIndex == colSubjectType.Index)
                            if (cell.Value == null)
                                cell.Value = "";

                        // 科目名稱與學分數
                        if (cell.ColumnIndex == colSubjectName.Index || cell.ColumnIndex == colCredit.Index)
                        {
                            if (cell.Value == null || cell.Value.ToString().Trim() == "")
                                cell.ErrorText = "請輸入資料!";

                        }

                        // 檢查課表
                        if (cell.ColumnIndex == colCourseTimetable.Index)
                        {
                            if (cell.Value == null || cell.Value.ToString() == "")
                                dgData.Rows[cell.RowIndex].ErrorText = "所屬課表必填!";
                        }

                    }

                }

                // 檢查資料是否有Error
                foreach (DataGridViewRow dr in dgData.Rows)
                {
                    if (dr.ErrorText != "")
                    {
                        FISCA.Presentation.Controls.MsgBox.Show("資料檢查有錯誤請修改!");
                        btnSave.Enabled = true;
                        return;
                    }

                    foreach (DataGridViewCell cell in dr.Cells)
                    {
                        if (cell.ErrorText != "")
                        {
                            FISCA.Presentation.Controls.MsgBox.Show("資料檢查有錯誤請修改!");
                            btnSave.Enabled = true;
                            return;
                        }
                    }
                }


                // 讀取需要新增或更新,並轉換資料
                foreach (DataGridViewRow row in dgData.Rows)
                {
                    if (row.IsNewRow)
                        continue;

                    UDTSubjectDef data = row.Tag as UDTSubjectDef;
                    if (data == null)
                        data = new UDTSubjectDef();

                    data.SchoolYear = _SchoolYear;
                    data.Semester = _Semester;
                    data.Month = _Month;

                    if (row.Cells[colDept.Index].Value != null)
                        data.DeptName = row.Cells[colDept.Index].Value.ToString();
                    else
                        data.DeptName = "";

                    data.SubjectName = row.Cells[colSubjectName.Index].Value.ToString();
                    if (row.Cells[colSubjectLevel.Index].Value != null && row.Cells[colSubjectLevel.Index].Value.ToString() == "")
                        data.SubjecLevel = null;
                    else
                        data.SubjecLevel = int.Parse(row.Cells[colSubjectLevel.Index].Value.ToString());

                    if(row.Cells[colCredit.Index].Value !=null)
                        data.Credit = int.Parse(row.Cells[colCredit.Index].Value.ToString());

                    if(row.Cells[colSubjectType.Index].Value!=null)
                        data.SubjectType = row.Cells[colSubjectType.Index].Value.ToString();

                    if (row.Cells[colCourseTimetable.Index].Value != null)
                        foreach (CourseTableDept cc in _AllCourseTableDeptList.Where(x => x.CourseTableName == row.Cells[colCourseTimetable.Index].Value.ToString()))
                            data.CourseTimetableID = cc.CourseTableID;

                    // 處理節次
                    XElement elmRoot = new XElement("Periods");
                    int per = 1;
                    for (int i = colWp1.Index; i <= colWp8.Index; i++)
                    {
                        if (row.Cells[i].Value != null && row.Cells[i].Value.ToString() != "")
                        {
                            XElement elm = new XElement("Period");
                            elm.SetValue(per);
                            elmRoot.Add(elm);
                        }
                        per++;
                    }
                    data.PeriodContent = "";
                    if (elmRoot.Elements().Count() > 0)
                    {
                        data.PeriodContent = elmRoot.ToString();
                    }

                    if (string.IsNullOrEmpty(data.UID))
                        _InsertDataList.Add(data);
                    else
                        _UpdateDataList.Add(data);
                }

                if (_DeleteDataList.Count > 0)
                    UDTTransfer.UDTSubjectDelete(_DeleteDataList);

                if (_InsertDataList.Count > 0)
                    UDTTransfer.UDTSubjectInsert(_InsertDataList);

                if (_UpdateDataList.Count > 0)
                    UDTTransfer.UDTSubjectUpdate(_UpdateDataList);

                btnSave.Enabled = true;
                _InsertDataList.Clear();
                _UpdateDataList.Clear();
                _DeleteDataList.Clear();
                FISCA.Presentation.Controls.MsgBox.Show("儲存完成");
                this.Close();

                #endregion
            }
            catch (Exception ex)
            {
                FISCA.Presentation.Controls.MsgBox.Show("儲存失敗," + ex.Message);
                btnSave.Enabled = true;
            }
        }
 /// <summary>
 /// 檢查是否可新增資料
 /// </summary>
 private bool CheckCanAddData(UDTSubjectDef data)
 {
     bool retVal = true;
     //if (data != null)
     //{
     //    string key = "";
     //    if (data.SubjecLevel.HasValue)
     //        key = data.SubjectName + "_" + data.SubjecLevel.Value + "_" + data.Credit;
     //    else
     //        key = data.SubjectName + "_" + data.Credit;
     //    if (_checkCanAddDict.ContainsKey(key))
     //        retVal = false;
     //    else
     //        _checkCanAddDict.Add(key, data);
     //}
     return retVal;
 }