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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<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">
<Grid>
<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>
<DataGridTextColumn Header="ID" Binding="{Binding CompanyId}" IsReadOnly="True"/>
<DataGridTextColumn Header="Название компании" Binding="{Binding Name}" Width="*"/>

17
eCompanies/MainWindow.xaml.cs

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

27
eCompanies/UsersWindow.xaml.cs

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