Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6.  
  7.  
  8. namespace SimpleWpfAppEDM
  9. {
  10.  
  11.     public partial class MainWindow : Window
  12.     {
  13.         ObservableCollection<Card> cards;
  14.  
  15.  
  16.         public MainWindow()
  17.         {
  18.             InitializeComponent();
  19.  
  20.             //загрузка данных
  21.             using (var context = new AutoLotEntities())
  22.             {
  23.                 var query = from card in context.Card
  24.                             select card;
  25.                 if (query.Count() != 0)
  26.                 {
  27.                     cards = new ObservableCollection<Card>();
  28.                     foreach (var c in query)
  29.                         cards.Add(c);
  30.                     lvCard.ItemsSource = cards;
  31.                 }
  32.             }
  33.  
  34.         }
  35.  
  36.  
  37.         //кнопка загрузки
  38.         private void BtLoad_Click(object sender, RoutedEventArgs e)
  39.         { }
  40.  
  41.         //кнопка добавить
  42.         private void BTAdd_Click(object sender, RoutedEventArgs e)
  43.         {
  44.             Card newCard = new Card();
  45.             EditCardView editCard = new EditCardView();
  46.             editCard.Title = "Добавление данных карты";
  47.             editCard.DataContext = newCard;
  48.             editCard.ShowDialog();
  49.             if (editCard.DialogResult == true)
  50.             {
  51.                 using (var context = new AutoLotEntities())
  52.                 {
  53.                     try
  54.                     {
  55.                         context.Card.Add(newCard);
  56.                         context.SaveChanges();
  57.                         cards.Add(newCard);
  58.                     }
  59.                     catch (Exception ex)
  60.                     {
  61.                         MessageBox.Show("\nОшибка добавления данных!\n" + ex.Message,
  62.                          "Предупреждение");
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.  
  69.  
  70.  
  71.         //кнопка редактировать
  72.         private void BtEdit_Click(object sender, RoutedEventArgs e)
  73.         {
  74.             Card editCard = (Card)lvCard.SelectedItem;
  75.             EditCardView editCardView = new EditCardView();
  76.             editCardView.Title = "Редактироание данных карты";
  77.             editCardView.DataContext = editCard;
  78.             editCardView.ShowDialog();
  79.             if (editCardView.DialogResult == true)
  80.             {
  81.                 using (var context = new AutoLotEntities())
  82.                 {
  83.                     try
  84.                     {
  85.                         Card card = context.Card.Find(editCard.Id);
  86.                         if (card.Make != editCard.Make)
  87.                             card.Make = editCard.Make.Trim();
  88.                         if (card.GB != editCard.GB)
  89.                             card.GB = editCard.GB.Trim();
  90.  
  91.                         context.SaveChanges();
  92.                     }
  93.                     catch (Exception ex)
  94.                     {
  95.                         MessageBox.Show("\nОшибка редактирования данных!\n" + ex.Message,
  96.                          "Предупреждение");
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.  
  102.         //кнопка удалить
  103.         private void BtDelete_Click(object sender, RoutedEventArgs e)
  104.         {
  105.             Card delCard = (Card)lvCard.SelectedItem;
  106.             using (var context = new AutoLotEntities())
  107.             {
  108.               try {
  109.                     Card delcard = context.Card.Find(delCard.Id);
  110.                     if (delcard != null)
  111.                     {
  112.                         MessageBoxResult result = MessageBox.Show("Удалить карту: \nПроизводитель: " + delCard.Make + "\nGB: " + delCard.GB, "Предупреждение", MessageBoxButton.OKCancel);
  113.                         if (result == MessageBoxResult.OK)
  114.                         {
  115.                                 context.Card.Remove(delcard);
  116.                                 context.SaveChanges();
  117.                                 cards.Remove(delCard);
  118.                         }
  119.                     }
  120.                   }
  121.               catch (Exception ex)
  122.               {
  123.                 MessageBox.Show("\nОшибка удаления данных!\n" + ex.Message, "Предупреждение");
  124.               }
  125.  
  126.             }
  127.  
  128.         }
  129.  
  130.         private void lvCard_SelectionChanged(object sender, SelectionChangedEventArgs e)
  131.         {
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137. System.Windows.Data Error: 40 : BindingExpression path error: 'Color' property not found on 'object' ''Card' (HashCode=17513507)'. BindingExpression:Path=Color; DataItem='Card' (HashCode=17513507); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')
  138. System.Windows.Data Error: 40 : BindingExpression path error: 'Color' property not found on 'object' ''Card' (HashCode=17513507)'. BindingExpression:Path=Color; DataItem='Card' (HashCode=17513507); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
  139. The program '[7828] SimpleWpfAppEDM.exe' has exited with code 0 (0x0).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement