Browse Source

Теперь удаление элементов из DataGrid происходит в виде скрытие. Реальное же удаление и сохранение в БД теперь будет выполняться после диалога сохранения.

master
Никита 6 years ago
parent
commit
bb431aff33
  1. 9
      eCompanies/App.xaml.cs
  2. 5
      eCompanies/CompaniesDBContext.cs
  3. 2
      eCompanies/Company.cs
  4. 16
      eCompanies/DataGridRowStatus.cs
  5. 28
      eCompanies/HiddenConverter.cs
  6. 2
      eCompanies/MainWindow.xaml
  7. 49
      eCompanies/MainWindow.xaml.cs
  8. 2
      eCompanies/Users.cs
  9. 2
      eCompanies/UsersWindow.xaml
  10. 34
      eCompanies/UsersWindow.xaml.cs
  11. 2
      eCompanies/eCompanies.csproj

9
eCompanies/App.xaml.cs

@ -42,15 +42,6 @@ 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())

5
eCompanies/CompaniesDBContext.cs

@ -38,7 +38,8 @@ namespace eCompanies
entity.Property(e => e.Name).IsRequired(); entity.Property(e => e.Name).IsRequired();
entity.Ignore("contract"); entity.Ignore("contract");
entity.Ignore("needUpdate");
entity.Ignore("dataGridRowStatus");
}); });
modelBuilder.Entity<Users>(entity => modelBuilder.Entity<Users>(entity =>
@ -57,6 +58,8 @@ namespace eCompanies
.WithMany(p => p.Users) .WithMany(p => p.Users)
.HasForeignKey(d => d.CompanyId) .HasForeignKey(d => d.CompanyId)
.HasConstraintName("FK_Users_Company"); .HasConstraintName("FK_Users_Company");
entity.Ignore("dataGridRowStatus");
}); });
} }
} }

2
eCompanies/Company.cs

@ -18,6 +18,6 @@ namespace eCompanies
public ICollection<Users> Users { get; set; } public ICollection<Users> Users { get; set; }
public bool needUpdate { get; set; } public DataGridRowStatus dataGridRowStatus { get; set; }
} }
} }

16
eCompanies/DataGridRowStatus.cs

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace eCompanies
{
public enum DataGridRowStatus
{
DEFAULT,
NEW,
UPDATED,
REMOVED
}
}

28
eCompanies/HiddenConverter.cs

@ -0,0 +1,28 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace eCompanies
{
[ValueConversion(typeof(DataGridRowStatus), typeof(Visibility))]
public class HiddenConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((DataGridRowStatus)value == DataGridRowStatus.REMOVED)
{
return Visibility.Collapsed;
}
else
{
return Visibility.Visible;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

2
eCompanies/MainWindow.xaml

@ -10,6 +10,7 @@
<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" CellEditEnding="companyGrid_CellEditEnding"> Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="companyGrid_CellEditEnding">
<DataGrid.Resources> <DataGrid.Resources>
<local:HiddenConverter x:Key="HiddenConverter"></local:HiddenConverter>
<ContextMenu x:Key="RowMenu" <ContextMenu x:Key="RowMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"> DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Редактировать" Click="RowEdit_Click"/> <MenuItem Header="Редактировать" Click="RowEdit_Click"/>
@ -19,6 +20,7 @@
<DataGrid.RowStyle> <DataGrid.RowStyle>
<Style TargetType="DataGridRow"> <Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" /> <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
<Setter Property="Visibility" Value="{Binding Path=dataGridRowStatus, Converter={StaticResource HiddenConverter}}"/>
</Style> </Style>
</DataGrid.RowStyle> </DataGrid.RowStyle>
<DataGrid.Columns> <DataGrid.Columns>

49
eCompanies/MainWindow.xaml.cs

@ -14,7 +14,6 @@ namespace eCompanies
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
UsersWindow usersWindowInstance = null; UsersWindow usersWindowInstance = null;
List<int> deletingCompanyList = new List<int>();
public MainWindow() public MainWindow()
{ {
@ -25,38 +24,26 @@ namespace eCompanies
private void updateCompanyItem(Company updatingCompany) private void updateCompanyItem(Company updatingCompany)
{ {
if (updatingCompany == null) return; if (updatingCompany == null) return;
updatingCompany.needUpdate = true;
} }
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
((Company)e.Row.Item).dataGridRowStatus = DataGridRowStatus.UPDATED;
}
private void RowEdit_Click(object sender, RoutedEventArgs e) private void RowEdit_Click(object sender, RoutedEventArgs e)
{ {
UpdateCompanyGridDelegate d = updateCompanyItem; UpdateCompanyGridDelegate d = updateCompanyItem;
Company company = (Company)companyGrid.CurrentItem; Company company = (Company)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close(); if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company, d); usersWindowInstance = new UsersWindow(company, d, deletingUsersList);
usersWindowInstance.Show(); usersWindowInstance.Show();
} }
private void RowDelete_Click(object sender, RoutedEventArgs e) private void RowDelete_Click(object sender, RoutedEventArgs e)
{ {
deleteCompanyFromGrid(); ((Company)companyGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
} companyGrid.Items.Refresh();
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) private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@ -87,18 +74,18 @@ namespace eCompanies
else if (mbr == MessageBoxResult.No) else if (mbr == MessageBoxResult.No)
return; return;
} }
if (c.CompanyId == 0) //if (c.CompanyId == 0)
App.addCompany(c); // App.addCompany(c);
else if (c.needUpdate) //else if (c.needUpdate)
App.updateCompany(c); // App.updateCompany(c);
} }
if (deletingCompanyList != null) //if (deletingCompanyList != null)
App.removeCompany(deletingCompanyList); // App.removeCompany(deletingCompanyList);
} //if (deletingUsersList != null)
// App.removeUser(deletingUsersList);
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) // нужно здесь обновить всех редактированных юзеров
{ // и добавить новых в базу данных
((Company)e.Row.Item).needUpdate = true; // TODO: переписать все обновления БД для [dataGridRowStatus]
} }
} }
} }

2
eCompanies/Users.cs

@ -9,5 +9,7 @@
public string Password { get; set; } public string Password { get; set; }
public Company Company { get; set; } public Company Company { get; set; }
public DataGridRowStatus dataGridRowStatus { get; set; }
} }
} }

2
eCompanies/UsersWindow.xaml

@ -24,10 +24,12 @@
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"> DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Удалить" Click="RowDelete_Click" /> <MenuItem Header="Удалить" Click="RowDelete_Click" />
</ContextMenu> </ContextMenu>
<local:HiddenConverter x:Key="HiddenConverter"></local:HiddenConverter>
</DataGrid.Resources> </DataGrid.Resources>
<DataGrid.RowStyle> <DataGrid.RowStyle>
<Style TargetType="DataGridRow"> <Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" /> <Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
<Setter Property="Visibility" Value="{Binding Path=dataGridRowStatus, Converter={StaticResource HiddenConverter}}"/>
</Style> </Style>
</DataGrid.RowStyle> </DataGrid.RowStyle>
</DataGrid> </DataGrid>

34
eCompanies/UsersWindow.xaml.cs

@ -14,9 +14,8 @@ namespace eCompanies
{ {
Company currentCompany; Company currentCompany;
UpdateCompanyGridDelegate updateCompanyItem; UpdateCompanyGridDelegate updateCompanyItem;
List<int> deletingUsersList = new List<int>();
public UsersWindow(Company c, UpdateCompanyGridDelegate d) public UsersWindow(Company c, UpdateCompanyGridDelegate d, List<int> deletingUsersList)
{ {
InitializeComponent(); InitializeComponent();
currentCompany = c; currentCompany = c;
@ -58,12 +57,12 @@ namespace eCompanies
return; return;
} }
if (deletingUsersList != null) if (u.UserId == 0)
App.removeUser(deletingUsersList); {
u.CompanyId = currentCompany.CompanyId;
u.CompanyId = currentCompany.CompanyId; u.Company = currentCompany;
u.Company = currentCompany; currentCompany.Users.Add(u);
currentCompany.Users.Add(u); }
} }
currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex; currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex;
@ -71,25 +70,10 @@ namespace eCompanies
updateCompanyItem(currentCompany); updateCompanyItem(currentCompany);
} }
private void deleteUserFromGrid()
{
var users = (ObservableCollection<Users>)usersGrid.DataContext;
foreach (Users u in users)
{
int deletingUser = ((Users)usersGrid.CurrentItem).UserId;
if (Equals(u.UserId, deletingUser))
{
deletingUsersList.Add(((Users)usersGrid.CurrentItem).UserId);
users.Remove(u);
usersGrid.DataContext = users;
return;
}
}
}
private void RowDelete_Click(object sender, RoutedEventArgs e) private void RowDelete_Click(object sender, RoutedEventArgs e)
{ {
deleteUserFromGrid(); ((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
usersGrid.Items.Refresh();
} }
} }
} }

2
eCompanies/eCompanies.csproj

@ -140,8 +140,10 @@
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="CompaniesDBContext.cs" /> <Compile Include="CompaniesDBContext.cs" />
<Compile Include="Contract.cs" /> <Compile Include="Contract.cs" />
<Compile Include="DataGridRowStatus.cs" />
<Compile Include="EnumBindingSourceExtension.cs" /> <Compile Include="EnumBindingSourceExtension.cs" />
<Compile Include="EnumDescriptoionTypeConverter.cs" /> <Compile Include="EnumDescriptoionTypeConverter.cs" />
<Compile Include="HiddenConverter.cs" />
<Compile Include="Users.cs" /> <Compile Include="Users.cs" />
<Compile Include="UsersWindow.xaml.cs"> <Compile Include="UsersWindow.xaml.cs">
<DependentUpon>UsersWindow.xaml</DependentUpon> <DependentUpon>UsersWindow.xaml</DependentUpon>

Loading…
Cancel
Save