using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using ManagementSystem.Utility; namespace ManagementSystem { public partial class AccountBook : Form { //程式內共用物件 string strFrmStatus = ""; //表單狀態 string strActiveUserID = ""; //取得登入作用的使用者帳號 SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString()); SqlCommand sqlCmd = new SqlCommand(); DataSet sdInsurance = new System.Data.DataSet(); string strPKey = ""; //程式內的Key值 string strKey = ""; //程式內加密的Key值 int intStartCount = 0; public AccountBook() { InitializeComponent(); } #region 自定義程式 private void SetupStatus() //畫面載入設定 { try { strActiveUserID = MainForm.strActiveUserID; strKey = MainForm.strKey; if (MainForm.strKey == "") { //設定Toolbar初始狀態 tsbAdd.Enabled = false; tsbSave.Enabled = false; tsbDelete.Enabled = false; } //設定畫面初始狀態 StatusChange("NONE"); } catch (Exception ex) { throw ex; } } private void CleanForm() //清除畫面 { //設定畫面物件狀態 //清除GridView string strSQL = "Select GUIDCode,InsuranceLevel,InsuranceAmountStart,InsuranceAmountEnd,C_LaborProtection,S_LaborProtection,C_HealthInsuranceAmount,S_HealthInsuranceAmount,RetirementAmount From OTB_HR_Insurance Order By InsuranceLevel"; sdInsurance =UtilityClass.GetSQLResult(strSQL); dgvDataMaintain.DataSource = sdInsurance.Tables["Result"]; Application.DoEvents(); } private void CleanToolbar() //清除工具列 { //設定Toolbar狀態 tsbSearch.Visible = false; tsbAdd.Visible = true; tsbEdit.Visible = true; tsbDelete.Visible = true; tsbSave.Visible = false; tsbOK.Visible = false; tsbCancel.Visible = false; } private void UnLockForm() //解除限制唯讀物件 { dgvDataMaintain.Columns["GUIDCODE"].ReadOnly = false; dgvDataMaintain.Columns["InsuranceLevel"].ReadOnly = false; dgvDataMaintain.Columns["InsuranceAmountStart"].ReadOnly = false; dgvDataMaintain.Columns["InsuranceAmountEnd"].ReadOnly = false; dgvDataMaintain.Columns["C_LaborProtection"].ReadOnly = false; dgvDataMaintain.Columns["S_LaborProtection"].ReadOnly = false; dgvDataMaintain.Columns["C_HealthInsuranceAmount"].ReadOnly = false; dgvDataMaintain.Columns["S_HealthInsuranceAmount"].ReadOnly = false; dgvDataMaintain.Columns["RetirementAmount"].ReadOnly = false; } private void LockForm() //限制唯讀物件 { dgvDataMaintain.AllowUserToAddRows = false; dgvDataMaintain.Columns["DelColumn"].ReadOnly = true; dgvDataMaintain.Columns["GUIDCODE"].ReadOnly = true; dgvDataMaintain.Columns["InsuranceLevel"].ReadOnly = true; dgvDataMaintain.Columns["InsuranceAmountStart"].ReadOnly = true; dgvDataMaintain.Columns["InsuranceAmountEnd"].ReadOnly = true; dgvDataMaintain.Columns["C_LaborProtection"].ReadOnly = true; dgvDataMaintain.Columns["S_LaborProtection"].ReadOnly = true; dgvDataMaintain.Columns["C_HealthInsuranceAmount"].ReadOnly = true; dgvDataMaintain.Columns["S_HealthInsuranceAmount"].ReadOnly = true; dgvDataMaintain.Columns["RetirementAmount"].ReadOnly = true; } private void StatusChange(string strStatus) //變更主畫面狀態 { switch (strStatus.ToUpper()) { case "NONE": CleanForm(); CleanToolbar(); LockForm(); dgvDataMaintain.AllowUserToAddRows = false; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = ""; strFrmStatus = ""; intStartCount = dgvDataMaintain.Rows.Count; break; case "SEARCH": UnLockForm(); dgvDataMaintain.DataSource = null; Application.DoEvents(); tsbSearch.Visible = false; tsbOK.Visible = true; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "搜尋"; strFrmStatus = "SEARCH"; break; case "ADD": UnLockForm(); dgvDataMaintain.AllowUserToAddRows = true; tsbAdd.Visible = false; tsbSave.Visible = true; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "新增"; strFrmStatus = "ADD"; break; case "MODIFY": UnLockForm(); tsbEdit.Visible = false; tsbSave.Visible = true; tsbCancel.Visible = true; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "修改"; strFrmStatus = "MODIFY"; break; case "DEL": dgvDataMaintain.Columns["DelColumn"].ReadOnly = false; tsbOK.Visible = true; tsbCancel.Visible = true; tsbDelete.Visible = false; ((MainForm)ParentForm).SsStatus.Items["tsslStatus"].Text = "刪除"; strFrmStatus = "DEL"; break; } } private bool CheckDel() //確認是否有資料被勾選刪除 { foreach (DataGridViewRow row in dgvDataMaintain.Rows) { DataGridViewCheckBoxCell Delcell = (DataGridViewCheckBoxCell)row.Cells["DelColumn"]; //刪除狀態列 DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["GUIDCode"]; //需刪除的Key值 if (Delcell.Value != null) { if (Delcell.Value == Delcell.TrueValue) { return true; } } } return false; } private void DelEven() //刪除事件 { try { string strDelItem = ""; if (CheckDel()) { if (MessageBox.Show("請問您確定要刪除本資料?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { foreach (DataGridViewRow row in dgvDataMaintain.Rows) { DataGridViewCheckBoxCell Delcell = (DataGridViewCheckBoxCell)row.Cells["DelColumn"]; //刪除狀態列 DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["GUIDCode"]; //需刪除的Key值 if (Delcell.Value != null) { if (Delcell.Value == Delcell.TrueValue) { if (strDelItem == "") { strDelItem = "'" + Keycell.Value.ToString() + "'"; } else { strDelItem += ",'" + Keycell.Value.ToString() + "'"; } } } } string strDelSQL = string.Format("Delete OTB_HR_Insurance Where GUIDCode in ({0})", strDelItem); string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString(); using (SqlConnection sqlConn = new SqlConnection(strConn)) { using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.DeleteCommand = new SqlCommand(); sqlAdapter.DeleteCommand.Connection = sqlConn; sqlAdapter.DeleteCommand.CommandText = strDelSQL.ToString(); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.DeleteCommand.ExecuteNonQuery(); MessageBox.Show("資料刪除失敗", "提示"); } } } } else { //未選取要刪除的資料 MessageBox.Show("請先選取欲刪除的資料", "提示"); } } catch (Exception ex) { throw ex; } } private void AddEven() //新增事件 { try { string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString(); int intDataCount = dgvDataMaintain.Rows.Count - 1; StringBuilder strSQL = new StringBuilder(); for (int intCount = intStartCount; intCount < intDataCount; intCount++) { strSQL.Append("Insert Into OTB_HR_Insurance(GUIDCode,InsuranceLevel,InsuranceAmountStart,InsuranceAmountEnd,C_LaborProtection,S_LaborProtection,C_HealthInsuranceAmount,S_HealthInsuranceAmount,RetirementAmount,CreateDate,CreateUser,ModifyDate,ModifyUser)"); strSQL.Append("Values (NEWID(),"); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["InsuranceLevel"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["InsuranceAmountStart"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["InsuranceAmountEnd"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["C_LaborProtection"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["S_LaborProtection"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["C_HealthInsuranceAmount"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["S_HealthInsuranceAmount"].Value.ToString() + ","); strSQL.Append(dgvDataMaintain.Rows[intCount].Cells["RetirementAmount"].Value.ToString() + ","); strSQL.Append("Getdate(),'" + strActiveUserID + "',Getdate(),'" + strActiveUserID + "') "); } using (SqlConnection sqlConn = new SqlConnection(strConn)) { using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.InsertCommand = new SqlCommand(); sqlAdapter.InsertCommand.Connection = sqlConn; sqlAdapter.InsertCommand.CommandText = strSQL.ToString(); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.InsertCommand.ExecuteNonQuery(); MessageBox.Show("資料新增成功", "提示"); StatusChange("NONE"); } } } catch (Exception ex) { MessageBox.Show("資料新增失敗", "提示"); throw ex; } } private void EditEven() //修改事件 { try { //宣告物件 string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString(); StringBuilder sbSQL = new StringBuilder(); foreach (DataGridViewRow row in dgvDataMaintain.Rows) { DataGridViewCheckBoxCell Updcell = (DataGridViewCheckBoxCell)row.Cells["UPDColumn"]; //修改狀態列 DataGridViewTextBoxCell Keycell = (DataGridViewTextBoxCell)row.Cells["GUIDCode"]; //需修改的Key值 if (Updcell.Value != null) { if (Updcell.Value == Updcell.TrueValue) { sbSQL.Append(" Update OTB_HR_Insurance Set "); sbSQL.Append(" InsuranceLevel = " + row.Cells["InsuranceLevel"].Value.ToString()); sbSQL.Append(" ,InsuranceAmountStart = " + row.Cells["InsuranceAmountStart"].Value.ToString()); sbSQL.Append(" ,InsuranceAmountEnd = " + row.Cells["InsuranceAmountEnd"].Value.ToString()); sbSQL.Append(" ,C_LaborProtection = " + row.Cells["C_LaborProtection"].Value.ToString()); sbSQL.Append(" ,S_LaborProtection = " + row.Cells["S_LaborProtection"].Value.ToString()); sbSQL.Append(" ,C_HealthInsuranceAmount = " + row.Cells["C_HealthInsuranceAmount"].Value.ToString()); sbSQL.Append(" ,S_HealthInsuranceAmount = " + row.Cells["S_HealthInsuranceAmount"].Value.ToString()); sbSQL.Append(" ,RetirementAmount = " + row.Cells["RetirementAmount"].Value.ToString()); sbSQL.Append(" ,ModifyDate = Getdate(), ModifyUser ='" + strActiveUserID + "'"); sbSQL.Append(" Where GUIDCode ='" + Keycell.Value.ToString() + "'"); } } } using (SqlConnection sqlConn = new SqlConnection(strConn)) { using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.UpdateCommand = new SqlCommand(); sqlAdapter.UpdateCommand.Connection = sqlConn; sqlAdapter.UpdateCommand.CommandText = sbSQL.ToString(); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.UpdateCommand.ExecuteNonQuery(); StatusChange("NONE"); } } MessageBox.Show("資料修改成功", "提示"); } catch (Exception ex) { MessageBox.Show("資料修改失敗", "提示"); throw ex; } } private void ShowUpdateDate(string strGUID) // 呈現最新修改人 { //排除空值 if (strGUID.Trim() == "") { return; } if (strGUID == null) { return; } strPKey = strGUID; string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConn"].ToString(); string strSQL = "Select ModifyDate, ModifyUser From OTB_HR_Insurance Where GUIDCode = '" + strPKey + "'"; using (SqlConnection sqlConn = new SqlConnection(strConn)) { using (SqlDataAdapter sqlAdapter = new SqlDataAdapter()) { //添加參數 sqlAdapter.SelectCommand = new SqlCommand(); sqlAdapter.SelectCommand.Connection = sqlConn; sqlAdapter.SelectCommand.CommandText = strSQL; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } //進行查詢 DataTable dtTemp = UtilityClass.GetSQLResult(strSQL).Tables["Result"]; ((MainForm)ParentForm).SsStatus.Items["tsslModifyUser"].Text = dtTemp.Rows[0]["ModifyUser"].ToString().Trim(); ((MainForm)ParentForm).SsStatus.Items["tsslModifyDate"].Text = dtTemp.Rows[0]["ModifyDate"].ToString().Trim(); } } } #endregion #region 事件觸發及問題處理 private void InsuranceMaintain_Load(object sender, EventArgs e) { SetupStatus(); } private void tsbEdit_Click(object sender, EventArgs e) { StatusChange("NONE"); StatusChange("MODIFY"); } private void tsbCancel_Click(object sender, EventArgs e) { StatusChange("NONE"); } private void tsbAdd_Click(object sender, EventArgs e) { StatusChange("NONE"); StatusChange("ADD"); } private void tsbDelete_Click(object sender, EventArgs e) { StatusChange("DEL"); } private void tsbOK_Click(object sender, EventArgs e) { try { StringBuilder strSQL = new StringBuilder(""); switch (strFrmStatus.ToString()) { case "DEL": //刪除 DelEven(); StatusChange("NONE"); break; } LockForm(); } catch (Exception ex) { throw ex; } } private void tsbSearch_Click(object sender, EventArgs e) { StatusChange("SEARCH"); } private void tsbSave_Click(object sender, EventArgs e) { try { StringBuilder strSQL = new StringBuilder(""); switch (strFrmStatus.ToString()) { case "ADD": //新增 AddEven(); break; case "MODIFY": //修改 EditEven(); break; } LockForm(); } catch (Exception ex) { throw ex; } } private void dgvDataMaintain_CellClick(object sender, DataGridViewCellEventArgs e) { ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["GUIDCode"].Value.ToString()); //顯示最新修改資料 } private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e) { ShowUpdateDate(dgvDataMaintain.CurrentRow.Cells["GUIDCode"].Value.ToString()); //顯示最新修改資料 } private void tsbExit_Click(object sender, EventArgs e) { this.Close(); } private void dgvDataMaintain_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dgvDataMaintain.CurrentRow != null) { DataGridViewCheckBoxCell UPDcell = (DataGridViewCheckBoxCell)dgvDataMaintain.CurrentRow.Cells["UPDColumn"]; //被修改的Key值 UPDcell.Value = UPDcell.TrueValue; } } #endregion } }