private void btnEdit_Click(object sender, EventArgs e) { // такого быть не должно, но лучше перестраховаться if (DataGridView1.CurrentRow == null || DataGridView1.CurrentRow.Tag == null) { this.UpdateButtons(); return; } var editor = new MoneyEditor((MoneyItem)DataGridView1.CurrentRow.Tag); editor.Owner = this.ParentForm; var result = editor.ShowDialog(); if (result == DialogResult.OK) { // обновляем строку this.UpdateRow(this.DataGridView1.CurrentRow, editor.MoneyItem); DataGridView1_CellEnter(DataGridView1, new DataGridViewCellEventArgs(0, this.DataGridView1.CurrentRow.Index)); // вычитаем старую сумму if (this.TotalAmountByCurrencies.ContainsKey(editor.PrevCurrencyCode)) { this.TotalAmountByCurrencies[editor.PrevCurrencyCode] -= editor.PrevAmount; } // добавляем новую сумму if (!this.TotalAmountByCurrencies.ContainsKey(this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode)) { this.TotalAmountByCurrencies.Add(this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode, 0); } this.TotalAmountByCurrencies[this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode] += editor.MoneyItem.Amount; // обновляем статус this.UpdateStatus(); } else if (result == DialogResult.Abort) { // запись была удалена // удаляем из списка DataGridView1.Rows.Remove(DataGridView1.CurrentRow); this.TotalAmountByCurrencies[this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode] -= editor.MoneyItem.Amount; this.UpdateStatus(); } }
private void btnAdd_Click(object sender, EventArgs e) { var editor = new MoneyEditor ( new MoneyItem { EntryType = this.ItemsType, AccountId = ((Account)this.Accounts.SelectedItem).Id, CategoryId = (((Category)this.Subcategories.SelectedItem).Id > 0 ? ((Category)this.Subcategories.SelectedItem).Id : ((Category)this.Categories.SelectedItem).Id) } ); editor.Owner = this.ParentForm; var result = editor.ShowDialog(); if (result == DialogResult.OK) { // проверяем возможность вставки записи в текущий список (по фильтрам) bool canAdd = (((Account)this.Accounts.SelectedItem).Id == 0 || ((Account)this.Accounts.SelectedItem).Id == editor.MoneyItem.AccountId); if (canAdd) { canAdd = ((Category)this.Categories.SelectedItem).Id == 0; if (!canAdd) { canAdd = (this.User.Categories[editor.MoneyItem.CategoryId].ParentId == 0 && ((Category)this.Categories.SelectedItem).Id == editor.MoneyItem.CategoryId) || (this.User.Categories[editor.MoneyItem.CategoryId].ParentId != 0 && ((Category)this.Subcategories.SelectedItem).Id == editor.MoneyItem.CategoryId) || (this.User.Categories[editor.MoneyItem.CategoryId].ParentId != 0 && ((Category)this.Subcategories.SelectedItem).Id == 0 && ((Category)this.Categories.SelectedItem).Id == this.User.Categories[editor.MoneyItem.CategoryId].ParentId); } } if (canAdd) { canAdd = editor.MoneyItem.DateEntry.Date >= this.DateFrom.Value.Date && editor.MoneyItem.DateEntry.Date <= this.DateTo.Value.Date; } if (canAdd && Convertion.ToDecimal(this.AmountFrom.Text, null).HasValue) { canAdd = editor.MoneyItem.Amount >= Convertion.ToDecimal(this.AmountFrom.Text, null).Value; } if (canAdd && Convertion.ToDecimal(this.AmountTo.Text, null).HasValue) { canAdd = editor.MoneyItem.Amount <= Convertion.ToDecimal(this.AmountTo.Text, null).Value; } if (canAdd) { // создаем строку var row = this.CreateRow(editor.MoneyItem); // определяем место, в которое можно вставить строку int index = 0; for (int i = 0; i < DataGridView1.Rows.Count; i++) { index = i; if (((MoneyItem)DataGridView1.Rows[i].Tag).DateEntry.Date <= editor.MoneyItem.DateEntry.Date) { break; } } // вставляем строку DataGridView1.Rows.Insert(index, row); DataGridView1.CurrentCell = DataGridView1.Rows[index].Cells[0]; DataGridView1_CellEnter(DataGridView1, new DataGridViewCellEventArgs(0, index)); // добавляем сумму if (!this.TotalAmountByCurrencies.ContainsKey(this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode)) { this.TotalAmountByCurrencies.Add(this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode, 0); } this.TotalAmountByCurrencies[this.User.Accounts[editor.MoneyItem.AccountId].CurrencyCode] += editor.MoneyItem.Amount; // обновляем статус this.UpdateStatus(); } } }