12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace BookStore_
- {
-
- public partial class MainWindow : Window
- {
- private BookStoreContext _context;
- public MainWindow()
- {
- InitializeComponent();
- _context = new BookStoreContext();
- Load();
- }
- private void Load()
- {
- BookGrid.ItemsSource = _context.Books.ToList();
- }
- private void BtnUpdate_Click(object sender, RoutedEventArgs e)
- {
- if (BookGrid.SelectedItem is Book selectedBook)
- {
- if (!decimal.TryParse(TbPrice.Text, out decimal price))
- {
- return;
- }
- selectedBook.Name = TbName.Text;
- selectedBook.Price = price;
- selectedBook.Author = TbAuthor.Text;
- selectedBook.Category = TbCategory.Text;
- _context.SaveChanges();
- Load();
- }
- }
-
- private void BtnIsert_Click(object sender, RoutedEventArgs e)
- {
- if (!decimal.TryParse(TbPrice.Text, out decimal price))
- {
- return;
- }
- Book book = new()
- {
- Name = TbName.Text,
- Price = price,
- Author = TbAuthor.Text,
- Category = TbCategory.Text,
- };
- _context.Books.Add(book);
- _context.SaveChanges();
- Load();
- }
- private void BtnDelete_Click(object sender, RoutedEventArgs e)
- {
- if (BookGrid.SelectedItem is Book selectedbook)
- {
- _context.Books.Remove(selectedbook);
- _context.SaveChanges();
- Load();
- }
- }
-
- private void BookGrid_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
- {
- if (BookGrid.SelectedItem is Book selectedBook)
- {
- TbName.Text = selectedBook.Name;
- TbPrice.Text = selectedBook.Price.ToString();
- TbAuthor.Text = selectedBook.Author;
- TbCategory.Text = selectedBook.Category;
- }
- }
- }
- }
|