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. 28
      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)
{
using (var db = new CompaniesDBContext())

5
eCompanies/CompaniesDBContext.cs

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

2
eCompanies/Company.cs

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

49
eCompanies/MainWindow.xaml.cs

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

2
eCompanies/Users.cs

@ -9,5 +9,7 @@
public string Password { 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}}">
<MenuItem Header="Удалить" Click="RowDelete_Click" />
</ContextMenu>
<local:HiddenConverter x:Key="HiddenConverter"></local:HiddenConverter>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
<Setter Property="Visibility" Value="{Binding Path=dataGridRowStatus, Converter={StaticResource HiddenConverter}}"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>

28
eCompanies/UsersWindow.xaml.cs

@ -14,9 +14,8 @@ namespace eCompanies
{
Company currentCompany;
UpdateCompanyGridDelegate updateCompanyItem;
List<int> deletingUsersList = new List<int>();
public UsersWindow(Company c, UpdateCompanyGridDelegate d)
public UsersWindow(Company c, UpdateCompanyGridDelegate d, List<int> deletingUsersList)
{
InitializeComponent();
currentCompany = c;
@ -58,38 +57,23 @@ namespace eCompanies
return;
}
if (deletingUsersList != null)
App.removeUser(deletingUsersList);
if (u.UserId == 0)
{
u.CompanyId = currentCompany.CompanyId;
u.Company = currentCompany;
currentCompany.Users.Add(u);
}
}
currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex;
currentCompany.Name = companyNameBox.Text;
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)
{
deleteUserFromGrid();
((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
usersGrid.Items.Refresh();
}
}
}

2
eCompanies/eCompanies.csproj

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

Loading…
Cancel
Save