private void deleteBtn_Click(object sender, EventArgs e) { for (int selectionIndex = 0; selectionIndex < dataGridView1.SelectedRows.Count; selectionIndex++) { String expenseId = this.dataGridView1.SelectedRows[selectionIndex].Cells[0].Value.ToString(); DateTime date = DateTime.ParseExact(this.dataGridView1.SelectedRows[selectionIndex].Cells[1].Value.ToString(), "dd-MMM-yy", new CultureInfo("en-US")); XmlOperations.XmlSearch xmlNavigator = new XmlOperations.XmlSearch(); XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(SysTray.getXmlFile())) { xmlDoc.Load(SysTray.getXmlFile()); } else { MessageBox.Show("File '" + SysTray.getXmlFile() + "' could not be found here.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } XmlNode calenderNode = xmlNavigator.findFirstNode(xmlDoc.DocumentElement, "calender", new String[] { "day", "month", "year" }, new String[] { date.Day.ToString(), date.Month.ToString(), date.Year.ToString() }); XmlNode expense = xmlNavigator.findFirstNode(calenderNode, "id", expenseId); expense.ParentNode.RemoveChild(expense); xmlDoc.Save(SysTray.getXmlFile()); } base.searchBtn_Click(sender, e); }
protected virtual void searchBtn_Click(object sender, EventArgs e) { XmlOperations.XmlSearch xmlNavigator = new XmlOperations.XmlSearch(); if (!isFormValid()) { return; } storeInputValues(); XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(SysTray.getXmlFile())) { xmlDoc.Load(SysTray.getXmlFile()); } else { MessageBox.Show("File '" + SysTray.getXmlFile() + "' could not be found here.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } XmlElement docParent = xmlDoc.DocumentElement; double totalCost = 0; String[] calenderAttributeNames = new String[] { "day", "month", "year" }; initGridView(); for (DateTime searchDate = fromDate; searchDate.CompareTo(toDate) <= 0; searchDate=searchDate.AddDays(1)) { userInputPair["date"] = searchDate; XmlNode calenderNode = xmlNavigator.findFirstNode(docParent, "calender", calenderAttributeNames, new String[] { searchDate.Day.ToString(), searchDate.Month.ToString(), searchDate.Year.ToString() }); if (calenderNode == null) { //MessageBox.Show("Could not find any record with the given filter.", "Expense Mgmt", MessageBoxButtons.OK, MessageBoxIcon.Information); continue; } IEnumerator expenseNode = calenderNode.ChildNodes.GetEnumerator(); while(expenseNode.MoveNext()) { if (xmlNavigator.isNodeMatching((XmlNode)expenseNode.Current,userInputPair)) { //Code to insert row into datatable XPathNavigator navigateExpenseToDisplay = ((XmlNode)expenseNode.Current).CreateNavigator(); navigateExpenseToDisplay.MoveToChild(XPathNodeType.Element); string item = navigateExpenseToDisplay.Value; navigateExpenseToDisplay.MoveToNext(); string category = navigateExpenseToDisplay.Value; navigateExpenseToDisplay.MoveToNext(); string cost = navigateExpenseToDisplay.Value; totalCost += double.Parse(cost); navigateExpenseToDisplay.MoveToNext(); string comment = navigateExpenseToDisplay.Value; navigateExpenseToDisplay.MoveToNext(); string id = navigateExpenseToDisplay.Value; string date = searchDate.ToShortDateString().ToString(); dataGridView1.Rows.Add(new String[] {id, date, item, cost, category, comment}); } } } totalCostTxtBox.Text = totalCost+""; AvgCostTxtBox.Text = Math.Round(totalCost / (((toDate - fromDate).TotalDays)+1),2) + ""; }
protected override void searchBtn_Click(object sender, EventArgs e) { if (!isUpdateBtnClicked) { base.searchBtn_Click(sender, e); modifiedExpenseList = new ArrayList(); updateBtn.Enabled = false; dataGridView1.Columns["Date"].ReadOnly = true; return; } foreach (InvalidatedExpense invalidExpense in modifiedExpenseList) { expense = new Hashtable(); expense["date"] = DateTime.ParseExact(this.dataGridView1.Rows[invalidExpense.rowIndex].Cells[1].Value.ToString(), "dd-MMM-yy", new CultureInfo("en-US")); expense["item"] = this.dataGridView1.Rows[invalidExpense.rowIndex].Cells[2].Value.ToString(); expense["cost"] = double.Parse(this.dataGridView1.Rows[invalidExpense.rowIndex].Cells[3].Value.ToString()); expense["category"] = this.dataGridView1.Rows[invalidExpense.rowIndex].Cells[4].Value.ToString(); expense["comment"] = this.dataGridView1.Rows[invalidExpense.rowIndex].Cells[5].Value.ToString(); XmlOperations.XmlSearch xmlNavigator = new XmlOperations.XmlSearch(); XmlDocument xmlDoc = new XmlDocument(); if (File.Exists(SysTray.getXmlFile())) { xmlDoc.Load(SysTray.getXmlFile()); } else { MessageBox.Show("File '" + SysTray.getXmlFile() + "' could not be found here.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } XmlNode oldExpenseCalenderNode = xmlNavigator.findFirstNode(xmlDoc.DocumentElement, "calender", new String[] { "day", "month", "year" }, new String[] { ((DateTime)expense["date"]).Day.ToString(), ((DateTime)expense["date"]).Month.ToString(), ((DateTime)expense["date"]).Year.ToString() }); XmlNode oldExpense = xmlNavigator.findFirstNode(oldExpenseCalenderNode, "id", invalidExpense.expenseID); xmlNavigator.replaceElement(oldExpense, expense); xmlDoc.Save(SysTray.getXmlFile()); } }