Пример #1
0
        /// <summary>
        /// 按下儲存
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSaveButtonClick(EventArgs e)
        {
            bool canSave = true;

            dgv.EndEdit();
            // 檢查資料
            foreach (DataGridViewRow drv in dgv.Rows)
            {
                foreach (DataGridViewCell cell in drv.Cells)
                {
                    if (cell.ErrorText != "")
                    {
                        canSave = false;
                    }
                }
            }


            if (canSave)
            {
                try
                {
                    _InsertDataList.Clear();
                    _UpdateDataList.Clear();
                    _DeleteDataList.Clear();
                    _HasUIDList.Clear();

                    // 儲存資料到 UDT
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        if (row.IsNewRow)
                        {
                            continue;
                        }

                        DAO.UDT_CounselUserDefDataDef udd = new DAO.UDT_CounselUserDefDataDef();

                        // 資料轉型
                        if (row.Tag != null)
                        {
                            udd = (DAO.UDT_CounselUserDefDataDef)row.Tag;
                        }

                        udd.StudentID = int.Parse(PrimaryKey);
                        if (row.Cells[FieldName.Index].Value != null)
                        {
                            udd.FieldName = row.Cells[FieldName.Index].Value.ToString();
                        }

                        if (row.Cells[Value.Index].Value != null)
                        {
                            udd.Value = row.Cells[Value.Index].Value.ToString();
                        }

                        // 新增或更新
                        if (string.IsNullOrEmpty(udd.UID))
                        {
                            _InsertDataList.Add(udd);
                        }
                        else
                        {
                            _UpdateDataList.Add(udd);
                            _HasUIDList.Add(udd.UID);
                        }
                        //_LogTransfer.SetLogValue(udd.FieldName, udd.Value);
                    }



                    // 新增或更新至 UDT
                    if (_InsertDataList.Count > 0)
                    {
                        _UDTTransfer.InsertCounselUsereDefinfDataList(_InsertDataList);
                    }

                    if (_UpdateDataList.Count > 0)
                    {
                        _UDTTransfer.UpdateCounselUserDefineDataList(_UpdateDataList);
                    }



                    // 刪除舊資料 UDT
                    foreach (DAO.UDT_CounselUserDefDataDef udd in _UserDefineData)
                    {
                        if (!_HasUIDList.Contains(udd.UID))
                        {
                            _DeleteDataList.Add(udd);
                        }
                    }

                    if (_DeleteDataList.Count > 0)
                    {
                        _UDTTransfer.DeleteCounselUserDefineDataList(_DeleteDataList);
                    }

                    this.CancelButtonVisible = false;
                    this.SaveButtonVisible   = false;
                }
                catch (Exception ex)
                {
                    FISCA.Presentation.Controls.MsgBox.Show("儲存失敗!");
                }
            }
            else
            {
                this.CancelButtonVisible = true;
                this.SaveButtonVisible   = false;
            }
        }