Добавьте файлы проекта.
This commit is contained in:
21
musicschoolapp/Pages/CourseEditingPage.xaml
Normal file
21
musicschoolapp/Pages/CourseEditingPage.xaml
Normal file
@@ -0,0 +1,21 @@
|
||||
<Page x:Class="musicschoolapp.Pages.CourseEditingPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="CourseEditingPage">
|
||||
|
||||
<Grid>
|
||||
<DataGrid x:Name="dGridStudent" AutoGenerateColumns="False" SelectionChanged="dGridStudent_SelectionChanged" MouseDoubleClick="dGridStudent_MouseDoubleClick">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Имя" Binding="{Binding Name}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Описание" Binding="{Binding Description}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Продолжительность" Binding="{Binding Duration}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Цена" Binding="{Binding Price}"></DataGridTextColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Grid>
|
||||
</Page>
|
163
musicschoolapp/Pages/CourseEditingPage.xaml.cs
Normal file
163
musicschoolapp/Pages/CourseEditingPage.xaml.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.IO;
|
||||
using System.Data.Entity.Validation;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для StudentEditingPage.xaml
|
||||
/// </summary>
|
||||
public partial class CourseEditingPage : Page
|
||||
{
|
||||
musicschoolEntities1 mse = new musicschoolEntities1();
|
||||
DbSet<Student> dbContext_;
|
||||
List<Course> students1;
|
||||
bool adding = false;
|
||||
public CourseEditingPage(bool editing)
|
||||
{
|
||||
InitializeComponent();
|
||||
students1 = mse.Course.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
if(editing)
|
||||
{
|
||||
dGridStudent.IsReadOnly = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
dGridStudent.IsReadOnly = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int updateDB()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var student in students1)
|
||||
{
|
||||
var original = mse.Course.Find(student.CourseID);
|
||||
|
||||
if (original != null)
|
||||
{
|
||||
mse.Entry(original).CurrentValues.SetValues(student);
|
||||
}
|
||||
else
|
||||
{
|
||||
mse.Course.Add(student);
|
||||
}
|
||||
}
|
||||
int saveresult = mse.SaveChanges();
|
||||
students1 = mse.Course.ToList();
|
||||
dGridStudent.ItemsSource=students1;
|
||||
return saveresult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
students1 = mse.Course.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
private void dGridStudent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)sender;
|
||||
var cellInfo = grid.SelectedCells[grid.CurrentCell.Column.DisplayIndex];
|
||||
|
||||
if (cellInfo.Column.Header.ToString() == "Фото")
|
||||
{
|
||||
if (MessageBox.Show("Вы дважды кликнули по столбцу 'Фото'. Изменить?", "Вопрос", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.ShowDialog();
|
||||
if (mse.Course.Find((grid.SelectedItem as Course).CourseID) == null)
|
||||
{
|
||||
MessageBox.Show("Объект не создан.");
|
||||
grid.SelectedItem = -1;
|
||||
mse.Course.Add(dGridStudent.SelectedItem as Course);
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbEntityValidationException ex)
|
||||
{
|
||||
foreach (var entityValidationErrors in ex.EntityValidationErrors)
|
||||
{
|
||||
foreach (var validationError in entityValidationErrors.ValidationErrors)
|
||||
{
|
||||
MessageBox.Show("Свойство: " + validationError.PropertyName + " Ошибка: " + validationError.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("добавил");
|
||||
int index = grid.SelectedIndex;
|
||||
grid.SelectedIndex = -1;
|
||||
grid.SelectedIndex = index;
|
||||
dGridStudent_SelectionChanged(sender, new SelectionChangedEventArgs(DataGrid.SelectionChangedEvent, new Collection<object> { }, new Collection<object> { dGridStudent.SelectedItem }));
|
||||
}
|
||||
mse.SaveChanges();
|
||||
dGridStudent.ItemsSource = mse.Course.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void dGridStudent_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems != null && e.AddedItems.Count > 0)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
else if (e.RemovedItems != null && e.RemovedItems.Count > 0)
|
||||
{
|
||||
|
||||
Course selectedStudent = (e.RemovedItems[0] as Course);
|
||||
if (selectedStudent != null)
|
||||
{
|
||||
Course studentInDb = mse.Course.Find(selectedStudent.CourseID);
|
||||
if (studentInDb != null)
|
||||
{
|
||||
mse.Course.Remove(studentInDb);
|
||||
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
|
||||
MessageBox.Show("Запись, похоже, удалить нельзя.\n\n" + ex.InnerException);
|
||||
students1 = mse.Course.ToList();
|
||||
mse.Entry(studentInDb).State = System.Data.EntityState.Unchanged;
|
||||
|
||||
return;
|
||||
}
|
||||
students1.Remove(dGridStudent.SelectedItem as Course);
|
||||
dGridStudent.SelectedIndex = -1;
|
||||
MessageBox.Show("Удалено");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
28
musicschoolapp/Pages/EnrollmentEditingPage.xaml
Normal file
28
musicschoolapp/Pages/EnrollmentEditingPage.xaml
Normal file
@@ -0,0 +1,28 @@
|
||||
<Page x:Class="musicschoolapp.Pages.EnrollmentEditingPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="EnrollmentEditingPage">
|
||||
|
||||
<Grid>
|
||||
<DataGrid Margin="0,20,0,0" x:Name="dGridStudent" AutoGenerateColumns="False" SelectionChanged="dGridStudent_SelectionChanged" MouseDoubleClick="dGridStudent_MouseDoubleClick">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn IsReadOnly="True" Header="ID зачисления" Binding="{Binding EnrollmentID}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Имя студента" Binding="{Binding Student.FirstName}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Фамилия студента" Binding="{Binding Student.LastName}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="ID курса" Binding="{Binding Course.Name}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Дата зачисления" Binding="{Binding EnrollmentDate}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Проходной балл" Binding="{Binding Grade}"></DataGridTextColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<Button Content="Экспорт в Excel" HorizontalAlignment="Left" Margin="67,0,0,0" VerticalAlignment="Top" Click="ToExcelButton_OnClick"/>
|
||||
<ComboBox x:Name="combobox_courses" HorizontalAlignment="Left" Margin="270,-2,0,0" VerticalAlignment="Top" Width="117" Height="22" SelectionChanged="combobox_courses_SelectionChanged"/>
|
||||
<TextBox x:Name="textbox_search" HorizontalAlignment="Left" Margin="482,1,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
|
||||
<Label Content="Фильтр:" HorizontalAlignment="Left" Margin="212,-4,0,0" VerticalAlignment="Top"/>
|
||||
<Label Content="Поиск:" HorizontalAlignment="Left" Margin="424,-4,0,0" VerticalAlignment="Top"/>
|
||||
</Grid>
|
||||
</Page>
|
288
musicschoolapp/Pages/EnrollmentEditingPage.xaml.cs
Normal file
288
musicschoolapp/Pages/EnrollmentEditingPage.xaml.cs
Normal file
@@ -0,0 +1,288 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.IO;
|
||||
using System.Data.Entity.Validation;
|
||||
using System.Data.OleDb;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для StudentEditingPage.xaml
|
||||
/// </summary>
|
||||
public partial class EnrollmentEditingPage : Page
|
||||
{
|
||||
musicschoolEntities1 mse = new musicschoolEntities1();
|
||||
DbSet<Enrollment> dbContext_;
|
||||
bool isadmin_;
|
||||
List<Enrollment> students1;
|
||||
bool adding = false;
|
||||
public EnrollmentEditingPage(bool isadmin)
|
||||
{
|
||||
InitializeComponent();
|
||||
this.isadmin_ = isadmin;
|
||||
if(isadmin)
|
||||
{
|
||||
dGridStudent.IsReadOnly = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
isadmin_ = false;
|
||||
dGridStudent.IsReadOnly = true;
|
||||
}
|
||||
combobox_courses.ItemsSource = mse.Course.ToList();
|
||||
students1 = mse.Enrollment.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
}
|
||||
|
||||
|
||||
public int updateDB()
|
||||
{
|
||||
combobox_courses.SelectedIndex = -1;
|
||||
try
|
||||
{
|
||||
foreach (var student in students1)
|
||||
{
|
||||
var original = mse.Enrollment.Find(student.EnrollmentID);
|
||||
|
||||
if (original != null)
|
||||
{
|
||||
mse.Entry(original).CurrentValues.SetValues(student);
|
||||
}
|
||||
else
|
||||
{
|
||||
mse.Enrollment.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
int saveresult = mse.SaveChanges();
|
||||
students1 = mse.Enrollment.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
return saveresult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.InnerException.ToString());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
private void dGridStudent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)sender;
|
||||
var cellInfo = grid.SelectedCells[grid.CurrentCell.Column.DisplayIndex];
|
||||
|
||||
if (cellInfo.Column.Header.ToString() == "Фото")
|
||||
{
|
||||
if (MessageBox.Show("Вы дважды кликнули по столбцу 'Фото'. Изменить?", "Вопрос", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.ShowDialog();
|
||||
if (mse.Student.Find((grid.SelectedItem as Enrollment).EnrollmentID) == null)
|
||||
{
|
||||
MessageBox.Show("Объект не создан.");
|
||||
grid.SelectedItem = -1;
|
||||
mse.Enrollment.Add(dGridStudent.SelectedItem as Enrollment);
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbEntityValidationException ex)
|
||||
{
|
||||
foreach (var entityValidationErrors in ex.EntityValidationErrors)
|
||||
{
|
||||
foreach (var validationError in entityValidationErrors.ValidationErrors)
|
||||
{
|
||||
MessageBox.Show("Свойство: " + validationError.PropertyName + " Ошибка: " + validationError.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("добавил");
|
||||
int index = grid.SelectedIndex;
|
||||
grid.SelectedIndex = -1;
|
||||
grid.SelectedIndex = index;
|
||||
dGridStudent_SelectionChanged(sender, new SelectionChangedEventArgs(DataGrid.SelectionChangedEvent, new Collection<object> { }, new Collection<object> { dGridStudent.SelectedItem }));
|
||||
}
|
||||
mse.SaveChanges();
|
||||
dGridStudent.ItemsSource = mse.Enrollment.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void dGridStudent_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems != null && e.AddedItems.Count > 0)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
else if (e.RemovedItems != null && e.RemovedItems.Count > 0)
|
||||
{
|
||||
Enrollment selectedStudent = (e.RemovedItems[0] as Enrollment);
|
||||
if (selectedStudent != null)
|
||||
{
|
||||
Enrollment studentInDb = mse.Enrollment.Find(selectedStudent.EnrollmentID);
|
||||
if (studentInDb != null && (e.RemovedItems[0] as Enrollment).Student != null && (e.RemovedItems[0] as Enrollment).Course != null && isadmin_)
|
||||
{
|
||||
|
||||
if (MessageBox.Show("Удалить " + (e.RemovedItems[0] as Enrollment).Student.LastName + " (" + (e.RemovedItems[0] as Enrollment).Course.Name + ")", "Внимание",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
mse.Enrollment.Remove(studentInDb);
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
|
||||
MessageBox.Show("Запись, похоже, удалить нельзя.\n\n" + ex.InnerException);
|
||||
MessageBox.Show("запись была удалена только из таблицы, но сохранена в базе данных, перезапустите окно и продолжайте работать.");
|
||||
return;
|
||||
}
|
||||
students1.Remove(dGridStudent.SelectedItem as Enrollment);
|
||||
dGridStudent.SelectedIndex = -1;
|
||||
if ((e.RemovedItems[0] as Enrollment).Student != null)
|
||||
{
|
||||
MessageBox.Show("Удалено: " + (e.RemovedItems[0] as Enrollment).Student.LastName);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Удалена 1 запись.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void BtnEdit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
private void ToExcelButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var d = dGridStudent.ItemsSource.Cast<Enrollment>();
|
||||
var data = ToDataTable(d.ToList());
|
||||
var dialog = new SaveFileDialog();
|
||||
dialog.DefaultExt = ".xlsx";
|
||||
dialog.Filter = "Excel файл |*.xlsx";
|
||||
dialog.ShowDialog();
|
||||
if (dialog.FileName != String.Empty)
|
||||
{
|
||||
ToExcelFile(data, dialog.FileName);
|
||||
}
|
||||
}
|
||||
|
||||
public static DataTable ToDataTable<T>(List<T> items)
|
||||
{
|
||||
var dataTable = new DataTable(typeof(T).Name);
|
||||
|
||||
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
|
||||
dataTable.Columns.Add(prop.Name, type);
|
||||
}
|
||||
foreach (var item in items)
|
||||
{
|
||||
var values = new object[properties.Length];
|
||||
for (var i = 0; i < properties.Length; i++)
|
||||
{
|
||||
values[i] = properties[i].GetValue(item, null);
|
||||
}
|
||||
dataTable.Rows.Add(values);
|
||||
}
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
public static void ToExcelFile(DataTable dataTable, string filePath, bool overwriteFile = true)
|
||||
{
|
||||
if (File.Exists(filePath) && overwriteFile)
|
||||
File.Delete(filePath);
|
||||
|
||||
using (var connection = new OleDbConnection())
|
||||
{
|
||||
connection.ConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={filePath};" +
|
||||
"Extended Properties='Excel 12.0 Xml;HDR=YES;'";
|
||||
connection.Open();
|
||||
using (var command = new OleDbCommand())
|
||||
{
|
||||
command.Connection = connection;
|
||||
var columnNames = (from DataColumn dataColumn in dataTable.Columns select dataColumn.ColumnName).ToList();
|
||||
var tableName = !string.IsNullOrWhiteSpace(dataTable.TableName) ? dataTable.TableName : Guid.NewGuid().ToString();
|
||||
command.CommandText = $"CREATE TABLE [{tableName}] ({string.Join(",", columnNames.Select(c => $"[{c}] VARCHAR").ToArray())});";
|
||||
command.ExecuteNonQuery();
|
||||
foreach (DataRow row in dataTable.Rows)
|
||||
{
|
||||
var rowValues = (from DataColumn column in dataTable.Columns select (row[column] != null && row[column] != DBNull.Value) ? row[column].ToString() : string.Empty).ToList();
|
||||
command.CommandText = $"INSERT INTO [{tableName}]({string.Join(",", columnNames.Select(c => $"[{c}]"))}) VALUES ({string.Join(",", rowValues.Select(r => $"'{r}'").ToArray())});";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
connection.Close();
|
||||
}
|
||||
MessageBox.Show("Файл успешно сохранён в "+filePath);
|
||||
}
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void combobox_courses_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
dGridStudent.SelectedIndex = 0;
|
||||
|
||||
if (combobox_courses.SelectedItem != null)
|
||||
{
|
||||
dGridStudent.ItemsSource = students1.Where(x => x.CourseID == (combobox_courses.SelectedItem as Course).CourseID).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
dGridStudent.ItemsSource = students1;
|
||||
}
|
||||
}
|
||||
|
||||
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
dGridStudent.SelectedIndex = -1;
|
||||
if (textbox_search.Text != String.Empty && dGridStudent.SelectedItem == null)
|
||||
{
|
||||
if(combobox_courses.SelectedItem != null)
|
||||
{
|
||||
dGridStudent.ItemsSource = students1.Where(x => x.Student.LastName.Contains(textbox_search.Text) && x.Course == combobox_courses.SelectedItem as Course).ToList();
|
||||
}
|
||||
else
|
||||
dGridStudent.ItemsSource = students1.Where(x => x.Student.LastName.Contains(textbox_search.Text)).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
dGridStudent.ItemsSource = students1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
21
musicschoolapp/Pages/Instr.xaml
Normal file
21
musicschoolapp/Pages/Instr.xaml
Normal file
@@ -0,0 +1,21 @@
|
||||
<Page x:Class="musicschoolapp.Pages.Instr"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="Instr">
|
||||
|
||||
<Grid>
|
||||
<DataGrid x:Name="dGridStudent" AutoGenerateColumns="False" SelectionChanged="dGridStudent_SelectionChanged" MouseDoubleClick="dGridStudent_MouseDoubleClick">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="ID" Binding="{Binding Id_instrument}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Название" Binding="{Binding Name_instrument}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Дата приобретения" Binding="{Binding Date_receiving}"></DataGridTextColumn>
|
||||
<DataGridCheckBoxColumn Header="Личный?" Binding="{Binding isPersonal}"></DataGridCheckBoxColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Grid>
|
||||
</Page>
|
168
musicschoolapp/Pages/Instr.xaml.cs
Normal file
168
musicschoolapp/Pages/Instr.xaml.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.IO;
|
||||
using System.Data.Entity.Validation;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для StudentEditingPage.xaml
|
||||
/// </summary>
|
||||
public partial class Instr : Page
|
||||
{
|
||||
musicschoolEntities1 mse = new musicschoolEntities1();
|
||||
DbSet<Student> dbContext_;
|
||||
List<Instruments> students1;
|
||||
bool adding = false;
|
||||
public Instr()
|
||||
{
|
||||
InitializeComponent();
|
||||
//dbContext_ = dbContext;
|
||||
students1 = mse.Instruments.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int updateDB()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var student in students1)
|
||||
{
|
||||
var original = mse.Instruments.Find(student.Id_instrument);
|
||||
|
||||
if (original != null)
|
||||
{
|
||||
// Обновить запись в базе данных
|
||||
mse.Entry(original).CurrentValues.SetValues(student);
|
||||
}
|
||||
else
|
||||
{
|
||||
mse.Instruments.Add(student);
|
||||
}
|
||||
}
|
||||
students1 = mse.Instruments.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
int saveresult = mse.SaveChanges();
|
||||
|
||||
return saveresult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
private void dGridStudent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)sender;
|
||||
var cellInfo = grid.SelectedCells[grid.CurrentCell.Column.DisplayIndex];
|
||||
|
||||
if (cellInfo.Column.Header.ToString() == "Фото")
|
||||
{
|
||||
if (MessageBox.Show("Вы дважды кликнули по столбцу 'Фото'. Изменить?", "Вопрос", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.ShowDialog();
|
||||
if (mse.Instruments.Find((grid.SelectedItem as Instruments).Id_instrument) == null)
|
||||
{
|
||||
MessageBox.Show("Объект не создан.");
|
||||
grid.SelectedItem = -1;
|
||||
//return;
|
||||
mse.Instruments.Add(dGridStudent.SelectedItem as Instruments);
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbEntityValidationException ex)
|
||||
{
|
||||
foreach (var entityValidationErrors in ex.EntityValidationErrors)
|
||||
{
|
||||
foreach (var validationError in entityValidationErrors.ValidationErrors)
|
||||
{
|
||||
MessageBox.Show("Свойство: " + validationError.PropertyName + " Ошибка: " + validationError.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("добавил");
|
||||
int index = grid.SelectedIndex;
|
||||
grid.SelectedIndex = -1;
|
||||
grid.SelectedIndex = index;
|
||||
//dGridStudent.RaiseEvent();
|
||||
dGridStudent_SelectionChanged(sender, new SelectionChangedEventArgs(DataGrid.SelectionChangedEvent, new Collection<object> { }, new Collection<object> { dGridStudent.SelectedItem }));
|
||||
}
|
||||
mse.SaveChanges();
|
||||
dGridStudent.ItemsSource = mse.Instruments.ToList();
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
private void dGridStudent_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems != null && e.AddedItems.Count > 0)
|
||||
{
|
||||
|
||||
}
|
||||
else if (e.RemovedItems != null && e.RemovedItems.Count > 0)
|
||||
{
|
||||
|
||||
Instruments selectedStudent = (e.RemovedItems[0] as Instruments);
|
||||
if (selectedStudent != null)
|
||||
{
|
||||
Instruments studentInDb = mse.Instruments.Find(selectedStudent.Id_instrument);
|
||||
if (studentInDb != null)
|
||||
{
|
||||
mse.Instruments.Remove(studentInDb);
|
||||
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
|
||||
MessageBox.Show("Запись, похоже, удалить нельзя.\n\n" + ex.InnerException);
|
||||
students1 = mse.Instruments.ToList();
|
||||
mse.Entry(studentInDb).State = System.Data.EntityState.Unchanged;
|
||||
|
||||
return;
|
||||
}
|
||||
students1.Remove(dGridStudent.SelectedItem as Instruments);
|
||||
dGridStudent.SelectedIndex = -1;
|
||||
//students1 = mse.Student.ToList();
|
||||
MessageBox.Show("Удалено");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void BtnEdit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
30
musicschoolapp/Pages/MenuPage.xaml
Normal file
30
musicschoolapp/Pages/MenuPage.xaml
Normal file
@@ -0,0 +1,30 @@
|
||||
<Page x:Class="musicschoolapp.Pages.MenuPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="MenuPage">
|
||||
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="115*"></RowDefinition>
|
||||
<RowDefinition Height="38*"></RowDefinition>
|
||||
<RowDefinition Height="74*"></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"></ColumnDefinition>
|
||||
<ColumnDefinition Width="*"></ColumnDefinition>
|
||||
<ColumnDefinition Width="*"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="1" Text="Выберите режим работы" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22" Height="30" Width="254"/>
|
||||
<Button x:Name="courseButton" IsEnabled="False" Grid.Column="0" Grid.Row="1" Content="Курс" Margin="10,10,10,10" FontSize="16" Click="courseButton_Click"></Button>
|
||||
<Button x:Name="enrollmentButton" IsEnabled="False" Grid.Column="1" Grid.Row="1" Content="Зачисление" Margin="10,10,10,10" FontSize="16" Click="enrollmentButton_Click"></Button>
|
||||
<Button x:Name="studentButton" IsEnabled="False" Grid.Column="2" Grid.Row="1" Content="Студент" Margin="10,10,10,10" FontSize="16" Click="studentButton_Click"></Button>
|
||||
<Button x:Name="adminButton" IsEnabled="False" Grid.Row="2" Content="Пользователи" FontSize="16" Margin="0,10,0,0" Grid.Column="1" HorizontalAlignment="Center" Width="266" Height="52" VerticalAlignment="Top" Click="adminButton_Click"/>
|
||||
<Button x:Name="quickEnrollButton" Content="Быстрое зачисление" HorizontalAlignment="Left" Margin="15,12,0,0" Grid.Row="2" VerticalAlignment="Top" Height="50" Width="240" Click="quickEnrollButton_Click" FontSize="16"/>
|
||||
<Button x:Name="instrButton" Grid.Column="2" Content="Инструменты" HorizontalAlignment="Right" Margin="0,10,10,0" Grid.Row="2" VerticalAlignment="Top" Height="52" Width="247" FontSize="16" IsEnabled="False" Click="instrButton_Click"/>
|
||||
</Grid>
|
||||
</Page>
|
68
musicschoolapp/Pages/MenuPage.xaml.cs
Normal file
68
musicschoolapp/Pages/MenuPage.xaml.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для MenuPage.xaml
|
||||
/// </summary>
|
||||
public partial class MenuPage : Page
|
||||
{
|
||||
StudentEditingPage studentEditingPage;
|
||||
CourseEditingPage courseEditingPage;
|
||||
public EnrollmentEditingPage enrollmentEditingPage;
|
||||
Instr instr = new Instr();
|
||||
public MenuPage(StudentEditingPage s, CourseEditingPage c, EnrollmentEditingPage e, Instr i)
|
||||
{
|
||||
enrollmentEditingPage = e;
|
||||
courseEditingPage = c;
|
||||
studentEditingPage = s;
|
||||
instr = i;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void courseButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NavigationService.Navigate(courseEditingPage);
|
||||
}
|
||||
|
||||
private void studentButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NavigationService.Navigate(studentEditingPage);
|
||||
}
|
||||
|
||||
private void enrollmentButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NavigationService.Navigate(enrollmentEditingPage);
|
||||
}
|
||||
|
||||
private void quickEnrollButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NavigationService.Navigate(new QuickEnroll());
|
||||
}
|
||||
|
||||
private void instrButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
NavigationService.Navigate(instr);
|
||||
}
|
||||
|
||||
private void adminButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Process.Start("AdminEditingApp\\admin\\etc\\bin\\Debug\\etc.exe");
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
}
|
27
musicschoolapp/Pages/QuickEnroll.xaml
Normal file
27
musicschoolapp/Pages/QuickEnroll.xaml
Normal file
@@ -0,0 +1,27 @@
|
||||
<Page x:Class="musicschoolapp.Pages.QuickEnroll"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="QuickEnroll">
|
||||
|
||||
<Grid>
|
||||
<Grid>
|
||||
<DataGrid IsEnabled="False" CanUserAddRows="False" Margin="0,25,0,0" x:Name="dGridStudent" AutoGenerateColumns="False" SelectionChanged="dGridStudent_SelectionChanged">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="ID студента" IsReadOnly="True" Binding="{Binding StudentID}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Имя студента" IsReadOnly="True" Binding="{Binding FirstName}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Фамилия студента" IsReadOnly="True" Binding="{Binding LastName}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Проходной балл" Binding="{Binding grade}"></DataGridTextColumn>
|
||||
<DataGridCheckBoxColumn x:Name="checkEnroll" Header="Зачислить?" Binding="{Binding toEnroll}">
|
||||
</DataGridCheckBoxColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<Button x:Name="buttonStart" Content="Выполнить" HorizontalAlignment="Left" Margin="205,0,0,0" VerticalAlignment="Top" Width="68" Click="buttonStart_Click" IsCancel="True"/>
|
||||
</Grid>
|
||||
<ComboBox x:Name="comboboxCourse" HorizontalAlignment="Left" Margin="75,0,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboboxCourse_SelectionChanged"/>
|
||||
</Grid>
|
||||
</Page>
|
76
musicschoolapp/Pages/QuickEnroll.xaml.cs
Normal file
76
musicschoolapp/Pages/QuickEnroll.xaml.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для QuickEnroll.xaml
|
||||
/// </summary>
|
||||
public partial class QuickEnroll : Page
|
||||
{
|
||||
musicschoolEntities1 mse = new musicschoolEntities1();
|
||||
public QuickEnroll()
|
||||
{
|
||||
InitializeComponent();
|
||||
comboboxCourse.ItemsSource = mse.Course.ToList();
|
||||
comboboxCourse.DisplayMemberPath = "Name";
|
||||
dGridStudent.ItemsSource = mse.Student.ToList();
|
||||
}
|
||||
|
||||
private void dGridStudent_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
|
||||
if (dGridStudent.SelectedCells[3] != null)
|
||||
{
|
||||
|
||||
}
|
||||
foreach (var student in dGridStudent.ItemsSource)
|
||||
{
|
||||
var student_ = student as Student;
|
||||
if (true)
|
||||
{
|
||||
|
||||
if (student_.toEnroll)
|
||||
{
|
||||
Enrollment enrollment = new Enrollment();
|
||||
enrollment.StudentID = student_.StudentID;
|
||||
enrollment.CourseID = (comboboxCourse.SelectedItem as Course).CourseID;
|
||||
enrollment.EnrollmentDate = DateTime.Now;
|
||||
enrollment.Grade = student_.grade;
|
||||
mse.Enrollment.Add(enrollment);
|
||||
student_.toEnroll = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void buttonStart_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
MessageBox.Show("Добавлено: " + mse.SaveChanges());
|
||||
}
|
||||
|
||||
private void comboboxCourse_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
dGridStudent.IsEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
48
musicschoolapp/Pages/StudentEditingPage.xaml
Normal file
48
musicschoolapp/Pages/StudentEditingPage.xaml
Normal file
@@ -0,0 +1,48 @@
|
||||
<Page x:Class="musicschoolapp.Pages.StudentEditingPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:musicschoolapp.Pages"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="450" d:DesignWidth="800"
|
||||
Title="StudentEditingPage">
|
||||
|
||||
<Grid>
|
||||
<DataGrid x:Name="dGridStudent" AutoGenerateColumns="False" SelectionChanged="dGridStudent_SelectionChanged" MouseDoubleClick="dGridStudent_MouseDoubleClick">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="ID" Binding="{Binding StudentID}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Имя" Binding="{Binding FirstName}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Фамилия" Binding="{Binding LastName}"></DataGridTextColumn>
|
||||
<DataGridTemplateColumn x:Name="dcolumn" Header="Дата рождения" Width="130">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<DatePicker SelectedDate="{Binding BirthDate}"></DatePicker>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
<!--<DataGridTextColumn Header="Дата рождения" Binding="{Binding BirthDate}"></DataGridTextColumn>-->
|
||||
<DataGridTextColumn Header="Номер телефона" Binding="{Binding Phone}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="Почта" Binding="{Binding Email}"></DataGridTextColumn>
|
||||
<DataGridTextColumn Header="ID инструмента" Binding="{Binding Id_instrument}"></DataGridTextColumn>
|
||||
<DataGridTextColumn IsReadOnly="True" Header="Название инструмента" Binding="{Binding Instruments}"></DataGridTextColumn>
|
||||
<DataGridTemplateColumn x:Name="PhotoColumn" Header="Фото" Width="130">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Image Height="80" Width="80" VerticalAlignment="Center"
|
||||
Source="{Binding photo}"></Image>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
<DataGridTemplateColumn Width="auto">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Button Width="80" Height="40" Content="Редактировать"
|
||||
Name="BtnEdit" Click="BtnEdit_Click"></Button>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Grid>
|
||||
</Page>
|
170
musicschoolapp/Pages/StudentEditingPage.xaml.cs
Normal file
170
musicschoolapp/Pages/StudentEditingPage.xaml.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
using System.Data.Entity.Migrations;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Contexts;
|
||||
using System.Text;
|
||||
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.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using System.IO;
|
||||
using System.Data.Entity.Validation;
|
||||
|
||||
namespace musicschoolapp.Pages
|
||||
{
|
||||
/// <summary>
|
||||
/// Логика взаимодействия для StudentEditingPage.xaml
|
||||
/// </summary>
|
||||
public partial class StudentEditingPage : Page
|
||||
{
|
||||
musicschoolEntities1 mse = new musicschoolEntities1();
|
||||
DbSet<Student> dbContext_;
|
||||
List<Student> students1;
|
||||
bool adding = false;
|
||||
public StudentEditingPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
//dbContext_ = dbContext;
|
||||
students1= mse.Student.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
}
|
||||
|
||||
|
||||
public int updateDB()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var student in students1)
|
||||
{
|
||||
var original = mse.Student.Find(student.StudentID);
|
||||
|
||||
if (original != null)
|
||||
{
|
||||
// Обновить запись в базе данных
|
||||
mse.Entry(original).CurrentValues.SetValues(student);
|
||||
}
|
||||
else
|
||||
{
|
||||
mse.Student.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
int saveresult = mse.SaveChanges();
|
||||
students1 = mse.Student.ToList();
|
||||
dGridStudent.ItemsSource = students1;
|
||||
return saveresult;
|
||||
} catch(Exception ex)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
private void dGridStudent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
var grid = (DataGrid)sender;
|
||||
if(grid.CurrentCell.Column != null)
|
||||
{
|
||||
var cellInfo = grid.SelectedCells[grid.CurrentCell.Column.DisplayIndex];
|
||||
|
||||
if (cellInfo.Column.Header.ToString() == "Фото")
|
||||
{
|
||||
if (MessageBox.Show("Вы дважды кликнули по столбцу 'Фото'. Изменить?", "Вопрос", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
openFileDialog.ShowDialog();
|
||||
if (mse.Student.Find((grid.SelectedItem as Student).StudentID) == null)
|
||||
{
|
||||
MessageBox.Show("Объект не создан.");
|
||||
grid.SelectedItem = -1;
|
||||
//return;
|
||||
mse.Student.Add(dGridStudent.SelectedItem as Student);
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbEntityValidationException ex)
|
||||
{
|
||||
foreach (var entityValidationErrors in ex.EntityValidationErrors)
|
||||
{
|
||||
foreach (var validationError in entityValidationErrors.ValidationErrors)
|
||||
{
|
||||
MessageBox.Show("Свойство: " + validationError.PropertyName + " Ошибка: " + validationError.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("добавил");
|
||||
int index = grid.SelectedIndex;
|
||||
grid.SelectedIndex = -1;
|
||||
grid.SelectedIndex = index;
|
||||
dGridStudent_SelectionChanged(sender, new SelectionChangedEventArgs(DataGrid.SelectionChangedEvent, new Collection<object> { }, new Collection<object> { dGridStudent.SelectedItem }));
|
||||
}
|
||||
mse.Student.Find((grid.SelectedItem as Student).StudentID).photo = File.ReadAllBytes(openFileDialog.FileName);
|
||||
mse.SaveChanges();
|
||||
dGridStudent.ItemsSource = mse.Student.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
private void dGridStudent_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems != null && e.AddedItems.Count > 0)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
else if (e.RemovedItems != null && e.RemovedItems.Count > 0)
|
||||
{
|
||||
|
||||
Student selectedStudent = (e.RemovedItems[0] as Student);
|
||||
if (selectedStudent != null)
|
||||
{
|
||||
Student studentInDb = mse.Student.Find(selectedStudent.StudentID);
|
||||
if (studentInDb != null)
|
||||
{
|
||||
mse.Student.Remove(studentInDb);
|
||||
|
||||
try
|
||||
{
|
||||
mse.SaveChanges();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
|
||||
MessageBox.Show("Запись, похоже, удалить нельзя.\n\n" + ex.InnerException);
|
||||
students1 = mse.Student.ToList();
|
||||
mse.Entry(studentInDb).State = System.Data.EntityState.Unchanged;
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
students1.Remove(dGridStudent.SelectedItem as Student);
|
||||
dGridStudent.SelectedIndex = -1;
|
||||
MessageBox.Show("Удалено");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void BtnEdit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user