private void DeletetoolStripButton_Click(object sender, EventArgs e) { if (!this.Validate()) { MessageBox.Show("有資料錯誤, 請改好再作刪除操作!"); return; } this.IngredientBindingSource.EndEdit(); string str = this.IngredientIDTextBox.Text.Trim(); int ingredientID = 0; string name; try { ingredientID = Convert.ToInt32(str); name = nameTextBox.Text.Trim(); } catch { MessageBox.Show("要刪除的食材內碼必需是數字!"); return; } string strCodeName = "食材 <" + ingredientID.ToString() + ">" + name; if (MessageBox.Show("能刪除的食材必需是本年度從來沒有進過貨的\r\n按'確定', 開始載入並檢查全年度進貨單!", "刪除" + strCodeName, MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } if (!m_VoucherDetailLoaded) { try { VoucherAdapter.Fill(m_DataSet.Voucher); VoucherDetailAdapter.Fill(m_DataSet.VoucherDetail); } catch (Exception ex) { MessageBox.Show("載入客人進貨細項時,資料庫發生錯誤:" + ex.Message); return; } m_VoucherDetailLoaded = true; } foreach (var row in m_DataSet.VoucherDetail) { if (row.IsIngredientIDNull()) { continue; } if (row.IngredientID == ingredientID) { var voucher = row.VoucherRow; MessageBox.Show("進貨單" + voucher.ID.ToString() + " 己經進過" + strCodeName + " 無法刪除"); return; } } if (MessageBox.Show(strCodeName + " 本年度沒有進過此貨,可以被刪除\r\n按'確定' 刪除", strCodeName, MessageBoxButtons.OKCancel) == DialogResult.OK) { try { IngredientBindingSource.RemoveCurrent(); IngredientAdapter.Update(m_DataSet.Ingredient); } catch (Exception ex) { MessageBox.Show("刪除" + strCodeName + "及存檔過程錯誤:" + ex.Message); return; } MessageBox.Show("己刪除" + strCodeName + " 並存檔成功!"); return; } MessageBox.Show("沒有刪除 " + strCodeName); }
// User 要核可這張單子了, 先檢查和參考價是否相同 private void voucherDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (!checkMode) { return; // 查核模式才能核可 } DataGridView view = (DataGridView)sender; if (view.Columns[e.ColumnIndex].Name != "columnCheck") { return; } DataGridViewRow row = view.Rows[e.RowIndex]; DataGridViewCell cell = row.Cells[e.ColumnIndex]; if (cell.FormattedValueType != typeof(bool)) { return; } if ((bool)cell.FormattedValue != true) { return; } foreach (DataGridViewRow r in dgvVoucherDetail.Rows) { DataGridViewCell costCell = r.Cells["dgCostColumn"]; if (IsDataWrong(typeof(decimal), costCell.Value)) { if (r.IsNewRow) { continue; // 無資料的最後一列 } MessageBox.Show("小計資料有誤!"); goto Error; } DataGridViewCell ingredientIDCell = r.Cells["dgIngredientIDColumn"]; if (IsDataWrong(typeof(int), ingredientIDCell.Value)) { MessageBox.Show("有食材沒有輸入!"); goto Error; } object volume = r.Cells["dgVolumeColumn"].Value; if (IsDataWrong(typeof(decimal), volume) || (decimal)volume == 0m) { MessageBox.Show(ingredientIDCell.FormattedValue + " 量有問題,請重新輸入!", "", MessageBoxButtons.OK); goto Error; } Color color = costCell.Style.ForeColor; decimal price = (decimal)costCell.Value / (decimal)volume; if (color == Color.Green || color == Color.Red) { int id; if (int.TryParse(ingredientIDCell.Value.ToString(), out id)) { // VEDataSet.IngredientRow[] rows = (VEDataSet.IngredientRow[])vEDataSet.Ingredient.Select("Code=" + codeCell.Value.ToString()); var rows = from ro in m_DataSet.Ingredient where (ro.IngredientID == id) select ro; if (rows.Count() == 0) { MessageBox.Show("查不到<" + ingredientIDCell.FormattedValue + ">,無法比對價格!"); return; } var r0 = rows.First(); double price0 = 0; if (!r0.IsPriceNull()) { price0 = r0.Price; } DialogResult result = MessageBox.Show(ingredientIDCell.FormattedValue + "價格<" + PriceForHuman((double)price) + ">和參考價<" + PriceForHuman(price0) + ">不同,是否改變參考價?", "", MessageBoxButtons.YesNoCancel); if (result == DialogResult.No) { continue; } if (result == DialogResult.Cancel) { goto Error; } if (result == DialogResult.Yes) { r0.Price = (double)price; IngredientAdapter.Update(r0); } } } } return; Error: cell.Value = false; row.Selected = true; }