Browse Source

Добавлен DataGrid на MainWindow для взаимодействия с компаниями. Добавлено новое Window - UsersWindow с аналогичным DataGrid. Добавлен диалог сохранения данных отдельной компании. Теперь ContractStatus в БД имеет тип int.

master
Никита 6 years ago
parent
commit
237d5d99e2
  1. 5
      db-creation.sql
  2. 25
      eCompanies/App.xaml.cs
  3. 5
      eCompanies/Companies.cs
  4. 4
      eCompanies/CompaniesDBContext.cs
  5. 17
      eCompanies/MainWindow.xaml
  6. 26
      eCompanies/MainWindow.xaml.cs
  7. 29
      eCompanies/UsersWindow.xaml
  8. 44
      eCompanies/UsersWindow.xaml.cs
  9. 7
      eCompanies/eCompanies.csproj

5
db-creation.sql

@ -7,7 +7,7 @@ GO
CREATE TABLE [Companies] (
[CompanyId] int NOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL,
[ContractStatus] varchar(15) NOT NULL CHECK ([ContractStatus] IN('NotReachedYet', 'Reached', 'Terminated')),
[ContractStatus] int NOT NULL CHECK ([ContractStatus] IN(0, 1, 2)),
CONSTRAINT [PK_CompanyId] PRIMARY KEY ([CompanyId])
);
GO
@ -24,5 +24,6 @@ CREATE TABLE [Users] (
GO
INSERT INTO [Companies] (Name, ContractStatus) VALUES
('Defend Studio', 'NotReachedYet')
('Defend Studio', '0'),
('Yandex Music', '1')
GO

25
eCompanies/App.xaml.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Linq;
@ -14,5 +15,29 @@ namespace eCompanies
/// </summary>
public partial class App : Application
{
private void addCompany(Companies c)
{
using (var db = new CompaniesDBContext())
{
db.Companies.Add(new Companies { Name = "Unknown company", ContractStatus = (int) Status.NotReachedYet });
var count = db.SaveChanges();
}
}
public static ObservableCollection<Companies> getCompanies()
{
using (var db = new CompaniesDBContext())
return new ObservableCollection<Companies>(db.Companies.AsEnumerable<Companies>());
}
public static void updateCompany(Companies c)
{
using (var db = new CompaniesDBContext())
{
db.Companies.Update(c);
db.SaveChanges();
}
}
}
}

5
eCompanies/Companies.cs

@ -3,9 +3,9 @@ using System.Collections.Generic;
namespace eCompanies
{
public enum Status { NotReachedYet, Reached, Terminated }
public partial class Companies
{
public enum Status { NotReachedYet, Reached, Terminated }
public Companies()
{
Users = new HashSet<Users>();
@ -13,8 +13,7 @@ namespace eCompanies
public int CompanyId { get; set; }
public string Name { get; set; }
public string ContractStatus { get; set; }
public int ContractStatus { get; set; }
public ICollection<Users> Users { get; set; }
}
}

4
eCompanies/CompaniesDBContext.cs

@ -33,9 +33,7 @@ namespace eCompanies
entity.HasKey(e => e.CompanyId);
entity.Property(e => e.ContractStatus)
.IsRequired()
.HasMaxLength(15)
.IsUnicode(false);
.IsRequired();
entity.Property(e => e.Name).IsRequired();
});

17
eCompanies/MainWindow.xaml

@ -4,10 +4,23 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:eCompanies"
xmlns:core="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ObjectDataProvider x:Key="Status" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="local:Status"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="75,50,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="305" Width="625"/>
<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.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding CompanyId}" IsReadOnly="True"/>
<DataGridTextColumn Header="Название компании" Binding="{Binding Name}"/>
<DataGridComboBoxColumn x:Name="companyGridBox" Header="Состояние контракта" SelectedItemBinding="{Binding ContractStatus}" ItemsSource="{Binding Source={StaticResource Status}}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>

26
eCompanies/MainWindow.xaml.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -20,31 +21,24 @@ namespace eCompanies
/// </summary>
public partial class MainWindow : Window
{
UsersWindow usersWindowInstance = null;
public MainWindow()
{
InitializeComponent();
doSomethingWithDb();
addTextToBlock();
}
private void addTextToBlock(string str = "")
private void addTextToBlock()
{
textBlock.Text += ("\n" + str);
companyGrid.DataContext = App.getCompanies();
}
private void doSomethingWithDb()
private void companyGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
using (var db = new CompaniesDBContext())
{
db.Companies.Add(new Companies { Name = "Unknown company", ContractStatus = Companies.Status.NotReachedYet.ToString() });
var count = db.SaveChanges();
addTextToBlock(count + " records saved to database");
addTextToBlock();
addTextToBlock("All companies in database:");
foreach (var company in db.Companies)
addTextToBlock(" - " + company.Name);
}
Companies company = (Companies)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company);
usersWindowInstance.Show();
}
}
}

29
eCompanies/UsersWindow.xaml

@ -0,0 +1,29 @@
<Window x:Class="eCompanies.UsersWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:eCompanies"
mc:Ignorable="d"
Title="UsersWindow" Height="450" Width="800" Closing="Window_Closing">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="usersGrid" HorizontalAlignment="Left" Height="354" Margin="10,55,0,0" VerticalAlignment="Top" Width="772" ItemsSource="{Binding}" AutoGenerateColumns="False" MouseDoubleClick="usersGrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding UsersId}"/>
<DataGridTextColumn Header="Имя пользователя" Binding="{Binding Name}" />
<DataGridTextColumn Header="Логин" Binding="{Binding Login}" />
<DataGridTextColumn Header="Пароль" Binding="{Binding Password}" />
</DataGrid.Columns>
</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">
<ComboBoxItem>Еще не заключен</ComboBoxItem>
<ComboBoxItem>Заключен</ComboBoxItem>
<ComboBoxItem>Расторгнут</ComboBoxItem>
</ComboBox>
</Grid>
</Window>

44
eCompanies/UsersWindow.xaml.cs

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
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.Shapes;
namespace eCompanies
{
/// <summary>
/// Логика взаимодействия для UsersWindow.xaml
/// </summary>
public partial class UsersWindow : Window
{
Companies currentCompany;
public UsersWindow(Companies c)
{
InitializeComponent();
currentCompany = c;
companyNameBox.Text = c.Name;
contractStatusComboBox.SelectedIndex = c.ContractStatus;
usersGrid.DataContext = new ObservableCollection<Users>(c.Users.AsEnumerable<Users>());
}
private void usersGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mbr = MessageBox.Show("Вы закрываете окно с компанией. Хотите сохранить результаты?", "Сохранение изменений", MessageBoxButton.YesNo);
if (mbr == MessageBoxResult.Yes)
App.updateCompany(currentCompany);
}
}
}

7
eCompanies/eCompanies.csproj

@ -140,6 +140,9 @@
</ApplicationDefinition>
<Compile Include="CompaniesDBContext.cs" />
<Compile Include="Users.cs" />
<Compile Include="UsersWindow.xaml.cs">
<DependentUpon>UsersWindow.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -153,6 +156,10 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="UsersWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">

Loading…
Cancel
Save