Добавьте файлы проекта.
This commit is contained in:
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