You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Xml.Linq;
  11. using System.IO;
  12. using System.Configuration;
  13. using ManagementSystem.Utility;
  14. namespace ManagementSystem
  15. {
  16. public partial class CurrencySetting : Form
  17. {
  18. //變數宣告
  19. public string strOwnerForm = ""; //設定表單的擁有者
  20. string strXMLName = "CurrencySetting"; //XML的名稱
  21. public CurrencySetting()
  22. {
  23. InitializeComponent();
  24. }
  25. #region 自定義程式
  26. private void CleanForm()
  27. {
  28. dgvDataMaintain.Rows.Clear();
  29. }
  30. #endregion
  31. #region 事件觸發及問題處理
  32. private void btnCancle_Click(object sender, EventArgs e)
  33. {
  34. this.Close();
  35. }
  36. private void dgvDataMaintain_CellContentClick(object sender, DataGridViewCellEventArgs e)
  37. {
  38. }
  39. private void dgvDataMaintain_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
  40. {
  41. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"];
  42. if (cbCell.Items.Count == 0)
  43. {
  44. cbCell.DataSource = UtilityClass.GetSystemArgument("Currency", "zh-TW").Tables["Arguments"];
  45. cbCell.DisplayMember = "ArgumentValue";
  46. cbCell.ValueMember = "ArgumentID";
  47. }
  48. }
  49. private void dgvDataMaintain_CellEnter(object sender, DataGridViewCellEventArgs e)
  50. {
  51. DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"];
  52. if (cbCell.Items.Count == 0)
  53. {
  54. cbCell.DataSource = UtilityClass.GetSystemArgument("Currency", "zh-TW").Tables["Arguments"];
  55. cbCell.DisplayMember = "ArgumentValue";
  56. cbCell.ValueMember = "ArgumentID";
  57. }
  58. }
  59. private void btnClean_Click(object sender, EventArgs e)
  60. {
  61. CleanForm();
  62. }
  63. private void dgvDataMaintain_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  64. {
  65. //宣告物件
  66. string strErrorMsg = "";
  67. string strCurrency = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrency"].Value;
  68. string strCurrencyRate = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cCurrencyRate"].Value;
  69. if (!dgvDataMaintain.Rows[e.RowIndex].IsNewRow)
  70. {
  71. if ((string.IsNullOrEmpty(strCurrency.Trim()) ? "" : strCurrency) == "")
  72. {
  73. strErrorMsg = "請選擇幣別";
  74. }
  75. if ((string.IsNullOrEmpty(strCurrencyRate.Trim()) ? "" : strCurrencyRate) == "")
  76. {
  77. strErrorMsg = "請輸入匯率";
  78. }
  79. else if (!UtilityClass.IsNumber(strCurrencyRate.Trim()))
  80. {
  81. strErrorMsg = "匯率必須為數值";
  82. }
  83. }
  84. if (strErrorMsg != "")
  85. {
  86. dgvDataMaintain.Rows[e.RowIndex].ErrorText = strErrorMsg;
  87. e.Cancel = true;
  88. }
  89. }
  90. private void dgvDataMaintain_RowValidated(object sender, DataGridViewCellEventArgs e)
  91. {
  92. dgvDataMaintain.Rows[e.RowIndex].ErrorText = "";
  93. }
  94. private void btnConfirm_Click(object sender, EventArgs e)
  95. {
  96. try
  97. {
  98. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString();
  99. XElement xmlRoot = new XElement("Root");
  100. foreach (DataGridViewRow drData in dgvDataMaintain.Rows)
  101. {
  102. if (!drData.IsNewRow)
  103. {
  104. XElement xmlContent = new XElement("Currency");
  105. xmlRoot.Add(xmlContent);
  106. XElement xmlSub = new XElement("CurrencyID", drData.Cells[0].Value.ToString());
  107. xmlSub.Add(new XAttribute("CurrencyRate", drData.Cells[1].Value.ToString()));
  108. xmlContent.Add(xmlSub);
  109. }
  110. }
  111. if (!File.Exists(strXMLPath))
  112. {
  113. UtilityClass.CreateDir(strXMLPath);
  114. }
  115. xmlRoot.Save(strXMLPath + strXMLName + ".xml");
  116. MessageBox.Show("設定完成", "提示");
  117. this.Close();
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show("設定錯誤", "提示");
  122. ErrorHandler.WriteErrorLog("CurrencySetting.cs", ex);
  123. this.Close();
  124. }
  125. }
  126. private void CurrencySetting_Load(object sender, EventArgs e)
  127. {
  128. string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + strXMLName + ".xml";
  129. if (File.Exists(strXMLPath))
  130. {
  131. XDocument xmlContent = XDocument.Load(strXMLPath);
  132. foreach (XElement xmlData in xmlContent.Descendants("Currency"))
  133. {
  134. DataGridViewRow dgvRow = new DataGridViewRow();
  135. dgvRow.CreateCells(dgvDataMaintain);
  136. dgvRow.Cells[1].Value = xmlData.Element("CurrencyID").Value.ToString();
  137. dgvRow.Cells[2].Value = xmlData.Element("CurrencyID").Attribute("CurrencyRate").Value.ToString();
  138. dgvDataMaintain.Rows.Add(dgvRow);
  139. }
  140. }
  141. }
  142. #endregion
  143. }
  144. }