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.
 
 

104 lines
3.8 KiB

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace eCompanies
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public delegate void UpdateCompanyGridDelegate(Company updatingCompany);
public partial class MainWindow : Window
{
UsersWindow usersWindowInstance = null;
List<int> deletingCompanyList = new List<int>();
public MainWindow()
{
InitializeComponent();
companyGrid.DataContext = App.getCompanies();
}
private void updateCompanyItem(Company updatingCompany)
{
if (updatingCompany == null) return;
updatingCompany.needUpdate = true;
}
private void RowEdit_Click(object sender, RoutedEventArgs e)
{
UpdateCompanyGridDelegate d = updateCompanyItem;
Company company = (Company)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company, d);
usersWindowInstance.Show();
}
private void RowDelete_Click(object sender, RoutedEventArgs e)
{
deleteCompanyFromGrid();
}
private void deleteCompanyFromGrid()
{
var companies = (ObservableCollection<Company>)companyGrid.DataContext;
foreach (Company c in companies)
{
int deletingUser = ((Company)companyGrid.CurrentItem).CompanyId;
if (Equals(c.CompanyId, deletingUser))
{
deletingCompanyList.Add(((Company)companyGrid.CurrentItem).CompanyId);
companies.Remove(c);
companyGrid.DataContext = companies;
return;
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mbr = MessageBox.Show("Вы закрываете окно с компаниями. Хотите сохранить результаты?", "Сохранение изменений", MessageBoxButton.YesNoCancel);
switch (mbr)
{
case MessageBoxResult.No: return;
case MessageBoxResult.Cancel:
e.Cancel = true;
break;
}
foreach (Company c in (ObservableCollection<Company>)companyGrid.DataContext)
{
if (c.Name == null)
{
mbr = MessageBox.Show("Вы не заполнили поле [Название компании]! " +
"Вернуться к форме для исправления? (При ответе Нет все данные будут потеряны!)",
"Поле [Название компании] не может быть пустым!",
MessageBoxButton.YesNo);
if (mbr == MessageBoxResult.Yes)
{
e.Cancel = true;
return;
}
else if (mbr == MessageBoxResult.No)
return;
}
if (c.CompanyId == 0)
App.addCompany(c);
else if (c.needUpdate)
App.updateCompany(c);
}
if (deletingCompanyList != null)
App.removeCompany(deletingCompanyList);
}
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
((Company)e.Row.Item).needUpdate = true;
}
}
}