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 XMLSetting : Form { //變數宣告 public string strOwnerForm = ""; //設定表單的擁有者 public string strXMLName = ""; //XML的名稱 public string strAccountingBookID = ""; //取得作用中的帳本 public XMLSetting() { InitializeComponent(); } #region 自定義程式 public void ReturnAccountList(string strAccountList) { string[] strAccList = strAccountList.Split('|'); dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubID"].Value = strAccList[1].ToString(); dgvDataMaintain.Rows[Convert.ToInt32(strAccList[0])].Cells["cAccountingSubName"].Value = strAccList[2].ToString(); } 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) { //當GridView中的Button被按下 var SenderGrid = (DataGridView)sender; if (SenderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "XMLSetting"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } private void dgvDataMaintain_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dgvDataMaintain.Rows[e.RowIndex].Cells["cDC"]; if (cbCell.Items.Count == 0) { cbCell.DataSource = UtilityClass.GetSystemArgument("DCClass", "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["cDC"]; if (cbCell.Items.Count == 0) { cbCell.DataSource = UtilityClass.GetSystemArgument("DCClass", "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 strDC = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cDC"].Value; if (!dgvDataMaintain.Rows[e.RowIndex].IsNewRow) { if ((string.IsNullOrEmpty(strDC.Trim()) ? "" : strDC) == "") { 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("Accounting"); xmlRoot.Add(xmlContent); XElement xmlSub = new XElement("SubAccounting", drData.Cells[1].Value.ToString()); xmlSub.Add(new XAttribute("SubName", drData.Cells[2].Value.ToString())); xmlSub.Add(new XAttribute("DC", drData.Cells[3].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("XMLSetting.cs", ex); this.Close(); } } private void XMLSetting_Load(object sender, EventArgs e) { strAccountingBookID = MainForm.strAccountingBookID; string strXMLPath = ConfigurationManager.AppSettings["XMLFilePath"].ToString() + strXMLName + ".xml"; if (File.Exists(strXMLPath)) { XDocument xmlContent = XDocument.Load(strXMLPath); //foreach (var xmlData in xmlContent.Descendants("Accounting")) foreach (XElement xmlData in xmlContent.Descendants("Accounting")) { DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow.CreateCells(dgvDataMaintain); dgvRow.Cells[1].Value = xmlData.Element("SubAccounting").Value.ToString(); dgvRow.Cells[2].Value = xmlData.Element("SubAccounting").Attribute("SubName").Value.ToString(); dgvRow.Cells[3].Value = xmlData.Element("SubAccounting").Attribute("DC").Value.ToString(); dgvDataMaintain.Rows.Add(dgvRow); } } } #endregion private void dgvDataMaintain_CellEndEdit(object sender, DataGridViewCellEventArgs e) { //物件宣告 string strAccountingSubID = ""; string strAccountingSubName = ""; //Cell[0] : 選窗Button //Cell[1] : 科目代碼(cAccountingSubID) //Cell[2] : 科目名稱(cAccountingSubName) //Cell[3] : 摘要(cMemo) //Cell[4] : 借方金額(cDebit) //Cell[5] : 貸方金額(cCredit) //科目代碼變動 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubID") { strAccountingSubID = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value; if (string.IsNullOrEmpty(strAccountingSubID)) { return; } else { string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubID = '{0}'", strAccountingSubID); if (UtilityClass.IsExist(strCheckSQL)) { strAccountingSubName = UtilityClass.GetAccountingSubjectByID(strAccountingSubID, strAccountingBookID).Rows[0]["AccountingSubName"].ToString(); dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value = strAccountingSubName; } else { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.txtAccountingSubID.Text = strAccountingSubID; pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "XMLSetting"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } } //科目名稱變動 if (dgvDataMaintain.Columns[e.ColumnIndex].Name.ToString() == "cAccountingSubName") { strAccountingSubName = (string)dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubName"].Value; if (string.IsNullOrEmpty(strAccountingSubName)) { return; } else { string strCheckSQL = string.Format("Select * From OTB_FNC_AccountingSubjects Where AccountingSubName = '{0}'", strAccountingSubName); if (UtilityClass.IsExist(strCheckSQL)) { strAccountingSubID = UtilityClass.GetAccountingSubjectByName(strAccountingSubName).Rows[0]["AccountingSubID"].ToString(); dgvDataMaintain.Rows[e.RowIndex].Cells["cAccountingSubID"].Value = strAccountingSubID; } else { PickAccountingSubject pkItemForm = new PickAccountingSubject(); pkItemForm.txtAccountingSubName.Text = strAccountingSubName; pkItemForm.intIndex = e.RowIndex; pkItemForm.strOwnerForm = "XMLSetting"; pkItemForm.Owner = this; pkItemForm.StartPosition = FormStartPosition.CenterParent; pkItemForm.ShowDialog(); } } } } } }