Browse Source

Реализовано полноценное сохранение удаленных и редактированных объектов в MainWindow

master
Никита 6 years ago
parent
commit
dc47b4bcc4
  1. 11
      eCompanies/App.xaml.cs
  2. 1
      eCompanies/CompaniesDBContext.cs
  3. 3
      eCompanies/Company.cs
  4. 2
      eCompanies/MainWindow.xaml
  5. 26
      eCompanies/MainWindow.xaml.cs

11
eCompanies/App.xaml.cs

@ -24,7 +24,7 @@ namespace eCompanies
return new HashSet<Users>(db.Users.Where(b => b.CompanyId.Equals(id))); return new HashSet<Users>(db.Users.Where(b => b.CompanyId.Equals(id)));
} }
private void addCompany(Company c) public static void addCompany(Company c)
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
{ {
@ -42,6 +42,15 @@ namespace eCompanies
} }
} }
public static void updateCompany1(Company c)
{
//using (var db = new CompaniesDBContext())
//{
// var x = db.Company.Where(i => i.CompanyId.Equals(c.CompanyId));
// foreach (Company t in x) t = c;
//}
}
public static void removeCompany(int id) public static void removeCompany(int id)
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())

1
eCompanies/CompaniesDBContext.cs

@ -38,6 +38,7 @@ namespace eCompanies
entity.Property(e => e.Name).IsRequired(); entity.Property(e => e.Name).IsRequired();
entity.Ignore("contract"); entity.Ignore("contract");
entity.Ignore("needUpdate");
}); });
modelBuilder.Entity<Users>(entity => modelBuilder.Entity<Users>(entity =>

3
eCompanies/Company.cs

@ -16,5 +16,8 @@ namespace eCompanies
public string Name { get; set; } public string Name { get; set; }
public int ContractStatus { get; set; } public int ContractStatus { get; set; }
public ICollection<Users> Users { get; set; } public ICollection<Users> Users { get; set; }
public bool needUpdate { get; set; }
} }
} }

2
eCompanies/MainWindow.xaml

@ -8,7 +8,7 @@
Title="MainWindow" Height="450" Width="800" Closing="Window_Closing"> Title="MainWindow" Height="450" Width="800" Closing="Window_Closing">
<Grid> <Grid>
<DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top" <DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top"
Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False"> Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="companyGrid_CellEditEnding">
<DataGrid.Resources> <DataGrid.Resources>
<ContextMenu x:Key="RowMenu" <ContextMenu x:Key="RowMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"> DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">

26
eCompanies/MainWindow.xaml.cs

@ -2,17 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace eCompanies namespace eCompanies
{ {
@ -22,9 +13,8 @@ namespace eCompanies
public delegate void UpdateCompanyGridDelegate(Company updatingCompany); public delegate void UpdateCompanyGridDelegate(Company updatingCompany);
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
List<int> deletingCompanyList = new List<int>();
List<int> updatingCompanyList = new List<int>();
UsersWindow usersWindowInstance = null; UsersWindow usersWindowInstance = null;
List<int> deletingCompanyList = new List<int>();
public MainWindow() public MainWindow()
{ {
@ -35,7 +25,7 @@ namespace eCompanies
private void updateCompanyItem(Company updatingCompany) private void updateCompanyItem(Company updatingCompany)
{ {
if (updatingCompany == null) return; if (updatingCompany == null) return;
updatingCompanyList.Add(updatingCompany.CompanyId); updatingCompany.needUpdate = true;
var context = ((ObservableCollection<Company>)companyGrid.DataContext).ToList<Company>(); var context = ((ObservableCollection<Company>)companyGrid.DataContext).ToList<Company>();
for (int i = 0; i < context.Count; i++) for (int i = 0; i < context.Count; i++)
{ {
@ -43,7 +33,6 @@ namespace eCompanies
{ {
context[i] = updatingCompany; context[i] = updatingCompany;
companyGrid.DataContext = new ObservableCollection<Company>(context); companyGrid.DataContext = new ObservableCollection<Company>(context);
updatingCompanyList.Add(updatingCompany.CompanyId);
return; return;
} }
} }
@ -108,13 +97,18 @@ namespace eCompanies
else if (mbr == MessageBoxResult.No) else if (mbr == MessageBoxResult.No)
return; return;
} }
if (c.CompanyId == 0)
App.addCompany(c);
else if (c.needUpdate)
App.updateCompany(c);
}
if (deletingCompanyList != null) if (deletingCompanyList != null)
App.removeCompany(deletingCompanyList); App.removeCompany(deletingCompanyList);
deletingCompanyList = null;
// TODO: реализация обновления старых компаний и добавления новых
} }
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
((Company)e.Row.Item).needUpdate = true;
} }
} }
} }

Loading…
Cancel
Save