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
{
///
/// Логика взаимодействия для StudentEditingPage.xaml
///
public partial class EnrollmentEditingPage : Page
{
musicschoolEntities1 mse = new musicschoolEntities1();
DbSet dbContext_;
bool isadmin_;
List 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