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.Xml.Linq; using System.IO; using System.Configuration; using ManagementSystem.Utility; namespace ManagementSystem { public partial class CurrencySetting : Form { //變數宣告 public string strOwnerForm = ""; //設定表單的擁有者 string strXMLName = "CurrencySetting"; //XML的名稱 public CurrencySetting() { InitializeComponent(); } #region 自定義程式 private void CleanForm() { dgvDataMaintain.Rows.Clear(); } #endregion #region 事件觸發及問題處理 private void btnCancle_Click(object sender, EventArgs e) { this.Close(); } private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void dgvDataMaintain_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"]; if (cbCell.Items.Count == 0) { cbCell.DataSource = UtilityClass.GetSystemArgument("Currency", "zh-TW").Tables["Arguments"]; cbCell.DisplayMember = "ArgumentValue"; cbCell.ValueMember = "ArgumentID"; } } private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e) { DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"]; if (cbCell.Items.Count == 0) { cbCell.DataSource = UtilityClass.GetSystemArgument("Currency", "zh-TW").Tables["Arguments"]; cbCell.DisplayMember = "ArgumentValue"; cbCell.ValueMember = "ArgumentID"; } } private void btnClean_Click(object sender, EventArgs e) { CleanForm(); } private void dgvDataMaintain_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { //宣告物件 string strErrorMsg = ""; string strCurrency = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"].Value; string strCurrencyRate = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrencyRate"].Value; if (!dgvDataMaintain.Rows[e.RowIndex].IsNewRow) { if ((string.IsNullOrEmpty(strCurrency.Trim()) ? "" : strCurrency) == "") { strErrorMsg = "請選擇幣別"; } if ((string.IsNullOrEmpty(strCurrencyRate.Trim()) ? "" : strCurrencyRate) == "") { strErrorMsg = "請輸入匯率"; } else if (!UtilityClass.IsNumber(strCurrencyRate.Trim())) { strErrorMsg = "匯率必須為數值"; } } if (strErrorMsg != "") { dgvDataMaintain.Rows[e.RowIndex].ErrorText = strErrorMsg; e.Cancel = true; } } private void dgvDataMaintain_RowValidated(object sender, DataGridViewCellEventArgs e) { dgvDataMaintain.Rows[e.RowIndex].ErrorText = ""; } private void btnConfirm_Click(object sender, EventArgs e) { try { string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString(); XElement xmlRoot = new XElement("Root"); foreach (DataGridViewRow drData in dgvDataMaintain.Rows) { if (!drData.IsNewRow) { XElement xmlContent = new XElement("Currency"); xmlRoot.Add(xmlContent); XElement xmlSub = new XElement("CurrencyID", drData.Cells[0].Value.ToString()); xmlSub.Add(new XAttribute("CurrencyRate", drData.Cells[1].Value.ToString())); xmlContent.Add(xmlSub); } } if (!File.Exists(strXMLPath)) { UtilityClass.CreateDir(strXMLPath); } xmlRoot.Save(strXMLPath + strXMLName + ".xml"); MessageBox.Show("設定完成", "提示"); this.Close(); } catch (Exception ex) { MessageBox.Show("設定錯誤", "提示"); ErrorHandler.WriteErrorLog("CurrencySetting.cs", ex); this.Close(); } } private void CurrencySetting_Load(object sender, EventArgs e) { string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + strXMLName + ".xml"; if (File.Exists(strXMLPath)) { XDocument xmlContent = XDocument.Load(strXMLPath); foreach (XElement xmlData in xmlContent.Descendants("Currency")) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvDataMaintain); dgvRow.Cells[1].Value = xmlData.Element("CurrencyID").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("CurrencyID").Attribute("CurrencyRate").Value.ToString(); dgvDataMaintain.Rows.Add(dgvRow); } } } #endregion } }