using customTypes; using HotelPms.Data.Common; using HotelPms.Data.Common.Interface.Access; using HotelPms.DataAccessGrpc.Client; using HotelPms.Share.Util; using HotelPms.Share.Windows.Component; using HotelPms.Share.Windows.Util; using HotelPms.WinForm.Common; using HotelPms.WinForm.Common.Interface; using HotelPms.WinForm.Common.Util; using System.Data; namespace HotelPms.WinForm.Master { public partial class Demo : MasterBase, IMasterCtrl { #region ★★★★★ Declartions ★★★★★ private IDemo? demoAccess; #endregion #region ★★★★★ Property ★★★★★ #endregion #region ★★★★★ Class Event ★★★★★ public Demo(IDemo access) { InitializeComponent(); demoAccess = access; MasterCtrl = this; dgvData.Visible = false; } private void Form_Load(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; MasterCtrl.ShowData(); System.Diagnostics.Debug.WriteLine(dgvData.Rows.Count); if (dgvData.Rows.Count == 0) { SetNewMode(); } dgvData.Visible = true; Cursor = Cursors.Default; } #endregion #region ★★★★★ Control Event ★★★★★ #endregion #region ★★★★★ Private Function ★★★★★ #endregion #region ★★★★★ Public Function ★★★★★ public bool Delete() { int orgIndex = dgvData.CurrentRow.Index; int pID = CConvert.ToInt(GetCellValue("ID")); if (MessageBox.Show(string.Format(DelMsg + Environment.NewLine + "ID = {0}", pID), Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.No) { return false; } //排他チェック if (demoAccess.GetUpdateID(pID) != CConvert.ToInt(GetCellValue("UpdateID"))) { if (MessageBox.Show(LockMsg, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { MasterCtrl.ShowData(pID); m_ValueChangeListener.SetUpdateFormMode(UpdateFormMode.Normal); } return false; } Data.DataResult dataResult = demoAccess.Remove($"ID = {pID}"); if (dataResult.ErrNo != 0) { MessageBox.Show("削除に失敗しました。" + Environment.NewLine + $"{dataResult.ErrNo}.{dataResult.ErrData}", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } ShowData(orgIndex < dgvData.RowCount ? orgIndex : dgvData.RowCount - 1); if (dgvData.Rows.Count == 0) { SetNewMode(); } return true; } public bool IsCorrectEtc(object sender, bool isCheckAll, bool isShowErrMsg) { if (sender.Equals(txtID) && m_ValueChangeListener.CurUpdateMode == UpdateFormMode.Inputting) { if (demoAccess.Exists(CConvert.ToInt(txtID.Text))) { MessageBox.Show(ExistsMsg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); txtID.Focus(); return false; } } return true; } public bool Save() { if (m_ValueChangeListener.CurUpdateMode != UpdateFormMode.Inputting && m_ValueChangeListener.CurUpdateMode != UpdateFormMode.Edit) { return true; } //ソートがあるため、index正しくない int id = (dgvData.CurrentRow == null) ? 0 : CConvert.ToInt(GetCellValue("ID")); bool isNew = false; HotelPms.Data.Master.Demo item = null; UpdateFormMode orgMode = m_ValueChangeListener.CurUpdateMode; if (orgMode == UpdateFormMode.Edit) { item = demoAccess.GetItem(CConvert.ToInt(txtID.Text)); //排他チェック if(item.UpdateID != CConvert.ToInt(GetCellValue("UpdateID"))) { if(MessageBox.Show(LockMsg, this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { MasterCtrl.ShowData(id); m_ValueChangeListener.SetUpdateFormMode(UpdateFormMode.Normal); } return false; } } if (item == null) { item = new HotelPms.Data.Master.Demo(); isNew = true; } item.ID = CConvert.ToInt(txtID.Text); item.Name = txtName.Text; item.FInt = CConvert.ToInt(txtFInt.Text); item.FTinyInt = CConvert.ToInt(txtFTinyInt.Text); item.FSmallInt = CConvert.ToInt(txtFSmallInt.Text); item.FBit = CConvert.ToBool(txtFBit.Text); item.FFloat = CConvert.ToFloat(txtFFloat.Text); item.FDecimal = CConvert.ToDecimal(txtFDecimal.Text); item.FDate = new Date(CConvert.ToDateInt(txtFDate.Text)); item.UpdateDate = CConvert.ToTimestamp(CConvert.ToDateTime(txtUpdateDate.Text)); item.UpdateID++; Data.DataResult dataResult = isNew ? demoAccess.Add(item) : demoAccess.Update(item); MasterCtrl.ShowData(id); if (orgMode == UpdateFormMode.Inputting) { SetNewMode(); } m_ValueChangeListener.SetUpdateFormMode(orgMode == UpdateFormMode.Edit ? UpdateFormMode.Normal : UpdateFormMode.New); return dataResult.ErrNo == 0; } public void SetDispName(CTextBox sender) { if (sender.Equals(txtFBit)) { sender.MstNameCtrl.Text = CConvert.GetEnumDescription(CConvert.ToInt(sender.Text)); } } public void ShowData() { ShowData(0); } private int FindRowIndex(int id) { for (int i = 0; i < m_Data.Rows.Count; i++) { if (id == CConvert.ToInt(m_Data.Rows[i]["ID"])) { return i; } } return 0; } public void ShowData(int id) { try { m_DisableSelectionChanged = true; DataTable orgData = m_Data; string where = string.Empty; if (txtSearchKey.Text.Trim().Length > 0) { where += string.Format(" A.Name LIKE '%{0}%' ", txtSearchKey.Text); } m_Data = demoAccess.GetMasterGridData(where); //同期実行 dgvData.DataSource = m_Data; SetGridColNameDict(); SetGridStyle(); if (dgvData.Rows.Count > 0) { SetCellFocus(FindRowIndex(id), 0, true); ShowDetail(); } if (orgData != null) { orgData.Dispose(); orgData = null; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { m_DisableSelectionChanged = false; } } private void SetGridStyle() { DataGridViewColumn col = null; col = dgvData.Columns["ID"]; col.HeaderText = "ID"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; col = dgvData.Columns["Name"]; col.HeaderText = "名称"; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col.Width = 100; col = dgvData.Columns["FInt"]; col.HeaderText = "FInt"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["FTinyInt"]; col.HeaderText = "FTinyInt"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; col = dgvData.Columns["FSmallInt"]; col.HeaderText = "FSmallInt"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["FFloat"]; col.HeaderText = "FFloat"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["FBit"]; col.HeaderText = "FBit"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["FDecimal"]; col.HeaderText = "FDecimal"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["FDate"]; col.HeaderText = "FDate"; col.Width = 100; col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft; col = dgvData.Columns["UpdateDate"]; col.Visible = false; col = dgvData.Columns["UpdateID"]; col.Visible = false; } public void ShowDetail() { if (dgvData.CurrentRow == null) { return; } tbArray.DisbleValidating = true; int index = dgvData.CurrentRow.Index; txtName.Text = CConvert.ToString(GetCellValue("Name")); this.ActiveControl = txtName; tbArray.PerformEnter(txtName, null); txtID.Enabled = false; txtID.Text = GeneralSub.ToFormText(CConvert.ToInt(GetCellValue("ID"))); txtFInt.Text = GeneralSub.ToFormText(CConvert.ToInt(GetCellValue("FInt"))); txtFTinyInt.Text = GeneralSub.ToFormText(CConvert.ToInt(GetCellValue("FTinyInt"))); txtFSmallInt.Text = GeneralSub.ToFormText(CConvert.ToInt(GetCellValue("FSmallInt"))); txtFFloat.Text = CConvert.ToFloat(GetCellValue("FFloat")).ToString(); txtFBit.Text = CConvert.ToBool(GetCellValue("FBit")) ? "1" : "0"; SetDispName(txtFBit); txtFDecimal.Text = CConvert.ToDecimal(GetCellValue("FDecimal")).ToString(); txtFDate.Text = CConvert.ToDateString(GetCellValue("FDate")); txtUpdateDate.Text = GetCellValue("UpdateDate").ToString(); m_ValueChangeListener.SetUpdateFormMode(UpdateFormMode.Normal); tbArray.DisbleValidating = false; } public void ShowList(CTextBox sender) { DataTable data = null; if (sender.Equals(txtFBit)) { data = CConvert.GetEnumTypeList(); } if (data == null) { return; } string ret = HotelPms.Share.Windows.Tool.DataSelector.Execute(sender, data, new Size(250, 120), string.Empty, string.Empty, null, HotelPms.Share.Windows.Tool.DataSelector.Style.HideBottom); if (ret.Length == 0) { return; } sender.Text = ret; tbArray.PerformKeyDown(sender, new KeyEventArgs(Keys.Enter)); } #endregion } }