12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- namespace BookStore
- {
- public partial class MainWindow : Window
- {
- dbBookStoreEntities db = new dbBookStoreEntities();
- public MainWindow() {InitializeComponent(); Load();}
- public void Load() { BooksGrid.ItemsSource = db.Books.ToList(); }
- private void BtnInsert_Click(object sender, RoutedEventArgs e) {BIC();}
- public void BIC()
- {
- if (!decimal.TryParse(TbPrice.Text, out decimal price)) {return;}
- Book book = new Book()
- {
- Name = TbName.Text,
- Price = price,
- Author = TbAuthor.Text,
- Category = TbCategory.Text
- };
- db.Books.Add(book); db.SaveChanges(); Load();
- }
- private void BtnUpdate_Click(object sender, RoutedEventArgs e) {BUC();}
- public void BUC()
- {
- if (BooksGrid.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;
- db.SaveChanges(); Load();
- }
- }
- private void BtnDelete_Click(object sender, RoutedEventArgs e) {BDC();}
- public void BDC()
- {
- if (BooksGrid.SelectedItem is Book selectedBook)
- { db.Books.Remove(selectedBook); db.SaveChanges(); TbClear(); Load(); }
- }
- public void TbClear()
- { TbName.Text = ""; TbPrice.Text = ""; TbAuthor.Text = ""; TbCategory.Text = ""; }
- private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {BGSC();}
- public void BGSC()
- {
- if (BooksGrid.SelectedItem is Book selectedBook)
- {
- TbName.Text = selectedBook.Name;
- TbPrice.Text = selectedBook.Price.ToString();
- TbAuthor.Text = selectedBook.Author;
- TbCategory.Text = selectedBook.Category;
- }
- }
- }
- }
|