Browse Source

Реализация редактирования и удаления компаний и пользователей через конекстное меню.

master
Никита 6 years ago
parent
commit
cc91012904
  1. 19
      eCompanies/App.xaml.cs
  2. 16
      eCompanies/MainWindow.xaml
  3. 17
      eCompanies/MainWindow.xaml.cs
  4. 11
      eCompanies/UsersWindow.xaml
  5. 27
      eCompanies/UsersWindow.xaml.cs

19
eCompanies/App.xaml.cs

@ -41,5 +41,24 @@ namespace eCompanies
} }
} }
public static void removeCompany(Company c)
{
using (var db = new CompaniesDBContext())
{
db.Company.Remove(c);
db.SaveChanges();
}
}
public static void removeUser(int id)
{
using (var db = new CompaniesDBContext())
{
var x = db.Users.Where(s => s.UserId.Equals(id));
foreach (Users u in x) db.Users.Remove(u);
db.SaveChanges();
}
}
} }
} }

16
eCompanies/MainWindow.xaml

@ -6,8 +6,20 @@
xmlns:local="clr-namespace:eCompanies" xmlns:local="clr-namespace:eCompanies"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top" Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" MouseDoubleClick="companyGrid_MouseDoubleClick"> <DataGrid x:Name="companyGrid" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top" Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Resources>
<ContextMenu x:Key="RowMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Редактировать" Click="RowEdit_Click"/>
<MenuItem Header="Удалить" Click="RowDelete_Click"/>
</ContextMenu>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow" >
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding CompanyId}" IsReadOnly="True"/> <DataGridTextColumn Header="ID" Binding="{Binding CompanyId}" IsReadOnly="True"/>
<DataGridTextColumn Header="Название компании" Binding="{Binding Name}" Width="*"/> <DataGridTextColumn Header="Название компании" Binding="{Binding Name}" Width="*"/>

17
eCompanies/MainWindow.xaml.cs

@ -19,6 +19,7 @@ namespace eCompanies
/// <summary> /// <summary>
/// Логика взаимодействия для MainWindow.xaml /// Логика взаимодействия для MainWindow.xaml
/// </summary> /// </summary>
public delegate void UpdateCompanyGridDelegate();
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
UsersWindow usersWindowInstance = null; UsersWindow usersWindowInstance = null;
@ -33,17 +34,27 @@ namespace eCompanies
companyGrid.DataContext = App.getCompanies(); companyGrid.DataContext = App.getCompanies();
} }
private void companyGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) private void RowEdit_Click(object sender, RoutedEventArgs e)
{ {
try try
{ {
UpdateCompanyGridDelegate d = updateCompanyGrid;
Company company = (Company)companyGrid.CurrentItem; Company company = (Company)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close(); if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company); usersWindowInstance = new UsersWindow(company, d);
usersWindowInstance.Show(); usersWindowInstance.Show();
} }
catch { } catch { }
} }
private void RowDelete_Click(object sender, RoutedEventArgs e)
{
try
{
Company company = (Company)companyGrid.CurrentItem;
App.removeCompany(company);
}
catch { }
}
} }
} }

11
eCompanies/UsersWindow.xaml

@ -18,6 +18,17 @@
<DataGridTextColumn Header="Логин" Width="*" Binding="{Binding Login}" /> <DataGridTextColumn Header="Логин" Width="*" Binding="{Binding Login}" />
<DataGridTextColumn Header="Пароль" Width="*" Binding="{Binding Password}" /> <DataGridTextColumn Header="Пароль" Width="*" Binding="{Binding Password}" />
</DataGrid.Columns> </DataGrid.Columns>
<DataGrid.Resources>
<ContextMenu x:Key="RowMenu"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Удалить" Click="RowDelete_Click" />
</ContextMenu>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
</Style>
</DataGrid.RowStyle>
</DataGrid> </DataGrid>
<TextBox x:Name="companyNameBox" HorizontalAlignment="Left" Height="40" Margin="10,10,0,0" TextWrapping="Wrap" Text="none" VerticalAlignment="Top" Width="470" FontSize="22"/> <TextBox x:Name="companyNameBox" HorizontalAlignment="Left" Height="40" Margin="10,10,0,0" TextWrapping="Wrap" Text="none" VerticalAlignment="Top" Width="470" FontSize="22"/>
<ComboBox x:Name="contractStatusComboBox" HorizontalAlignment="Left" Height="40" Margin="490,10,0,0" VerticalAlignment="Top" Width="292" FontSize="22" ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}"> <ComboBox x:Name="contractStatusComboBox" HorizontalAlignment="Left" Height="40" Margin="490,10,0,0" VerticalAlignment="Top" Width="292" FontSize="22" ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Status}}}">

27
eCompanies/UsersWindow.xaml.cs

@ -13,34 +13,39 @@ namespace eCompanies
public partial class UsersWindow : Window public partial class UsersWindow : Window
{ {
Company currentCompany; Company currentCompany;
public UsersWindow(Company c) UpdateCompanyGridDelegate updateCompanyGrid;
public UsersWindow(Company c, UpdateCompanyGridDelegate d)
{ {
InitializeComponent(); InitializeComponent();
currentCompany = c; currentCompany = c;
currentCompany.Users = App.getUsers(currentCompany.CompanyId); updateCompanyGrid = d;
companyNameBox.Text = c.Name; updateUsersGrid();
contractStatusComboBox.SelectedIndex = c.ContractStatus;
usersGrid.DataContext = new ObservableCollection<Users>(c.Users.AsEnumerable());
} }
private void usersGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) private void updateUsersGrid()
{ {
currentCompany.Users = App.getUsers(currentCompany.CompanyId);
companyNameBox.Text = currentCompany.Name;
contractStatusComboBox.SelectedIndex = currentCompany.ContractStatus;
usersGrid.DataContext = new ObservableCollection<Users>(currentCompany.Users.AsEnumerable());
} }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ {
MessageBoxResult mbr = MessageBox.Show("Вы закрываете окно с компанией. Хотите сохранить результаты?", "Сохранение изменений", MessageBoxButton.YesNo); MessageBoxResult mbr = MessageBox.Show("Вы закрываете окно с компанией. Хотите сохранить результаты?", "Сохранение изменений", MessageBoxButton.YesNo);
if (mbr == MessageBoxResult.No) if (mbr == MessageBoxResult.No)
return; return;
currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex;
currentCompany.Name = companyNameBox.Text;
foreach (Users u in (ObservableCollection<Users>)usersGrid.DataContext) foreach (Users u in (ObservableCollection<Users>)usersGrid.DataContext)
{ {
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.Name = companyNameBox.Text;
App.updateCompany(currentCompany); App.updateCompany(currentCompany);
updateCompanyGrid();
} }
private void usersGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) private void usersGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
@ -50,5 +55,11 @@ namespace eCompanies
private void usersGrid_AddingNewItem(object sender, AddingNewItemEventArgs e) private void usersGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{ {
} }
private void RowDelete_Click(object sender, RoutedEventArgs e)
{
App.removeUser(((Users)usersGrid.CurrentItem).UserId);
updateUsersGrid();
}
} }
} }

Loading…
Cancel
Save