• 1405/06/22

تمرین جلسه 100 - ویرایش :

با سلام

 

using DataLayer.Context;
using DataLayer.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BookKeeper.Books
{
    public partial class frmEdit : Form
    {
        int _bookid;
        BookKeeper_DB_Context _context = new BookKeeper_DB_Context();
        public frmEdit(int bookId)
        {
            InitializeComponent();
            _bookid = bookId;

        }

        private void frmEdit_Load(object sender, EventArgs e)
        {
            if (_bookid != 0)
            {
                #region Instantiation


                var book = _context.Books.Single(q => q.Id == _bookid);

                #endregion 


                #region Populating

                txtBookName.Text = book.Title;
                txtAuthorName.Text = book.AuthorName;
                txtRating.Value = book.Rating;
                txtSubject.Text = book.Subject;

                #region Radio Button

                rbIsFavorite.Checked = book.IsFavorite;
                if (book.IsFavorite != true)
                {
                    rbNotFavorite.Checked = true;
                }

                rbIsWhishList.Checked = book.IsWishlist;
                if (book.IsWishlist != true)
                {
                    rbNotWhishList.Checked = true;
                }

                #endregion

                #endregion

             




            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void txtBookName_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnCancel.DialogResult = DialogResult.Cancel;
        }

        private void rbIsWhishList_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            var book = _context.Books.Single(q => _bookid == q.Id);
            book.Title = txtBookName.Text;
            book.AuthorName = txtAuthorName.Text;
            book.Subject = txtSubject.Text;
            book.Rating = Convert.ToInt32(txtRating.Value);
            #region RadioButton
            if (rbIsWhishList.Checked == false)
            {
                book.IsWishlist = false;
            }
            else
            {
                book.IsWishlist = true;
            }
            if (rbIsFavorite.Checked == false)
            {
                book.IsFavorite = false;
            }
            else
            {
                book.IsFavorite = true;
            }
            #endregion
            if (rbNotWhishList.Checked == false)
            {
                book.IsAvailable = true;
            }
            else
            {
                book.IsWishlist = false;
            }
            _context.Books.Update(book);
            _context.SaveChanges();

            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void frmEdit_Leave(object sender, EventArgs e)
        {

        }
    }
}
using Barnamenevis.Net.RtlMessageBox.WindowsForms;
using DataLayer.Context;
using DataLayer.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Tools.Convertor;
namespace BookKeeper.Books
{
    public partial class frmBookManagement : Form
    {
        BookKeeper_DB_Context bookKeeper = new();


        public frmBookManagement()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void frmBookManagement_Load(object sender, EventArgs e)
        {
            BindGrid();
        }

        #region Data Grid View Books
        void BindGrid()
        {
            dgvBooks.Rows.Clear();
            var book = bookKeeper.Books.AsNoTracking().ToList();
            foreach (var item in book)
            {
                dgvBooks.Rows.Add(item.Id,
                    item.Title,
                    item.Subject,
                    item.AuthorName,
                    item.Description,
                    item.Rating,
                    item.IsAvailable ? "موجود" : "ناموجود",
                    item.IsFavorite ? "⭐" : "-",
                    item.IsWishlist ? "📌" : "-",
                    item.AddedDate.ToShamsiDateOnly()
                    );
            }
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddBook frmAddBook = new frmAddBook();
            frmAddBook.ShowDialog();
            if (frmAddBook.DialogResult == DialogResult.Yes)
            {
                BindGrid();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvBooks.CurrentRow != null)
            {
                string id = dgvBooks.CurrentRow.Cells[0].Value.ToString();
                string bookName = dgvBooks.CurrentRow.Cells[1].Value.ToString();
                string authotName = dgvBooks.CurrentRow.Cells[3].Value.ToString();

                if (bookName == "CAN'T HURT ME")
                {
                    RtlMessageBox.Show("نمیتوانید از خط را پاک کنید", "خطا", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Stop);
                }
                else
                {
                    int bookId = Convert.ToInt32(id);
                    string message = $"آیا مطمئنی میخوای کتاب : '{bookName}' از نویسنده : '{authotName}' را پاک کنی؟ ";
                    var result = RtlMessageBox.Show(message, "سوال", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        var bookDelete = bookKeeper.Books.Single(b => b.Id == bookId);
                        bookKeeper.Books.Remove(bookDelete);
                        bookKeeper.SaveChanges();
                        BindGrid();

                    }

                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvBooks.CurrentRow != null)
            {
                string id = dgvBooks.CurrentRow.Cells[0].Value.ToString();
                int bookId = Convert.ToInt32(id);
                frmEdit frmEdit = new frmEdit(bookId);
                var result = frmEdit.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    BindGrid();
                }
                
             
               
            }
        }

    }
}
  • 1405/06/22
  • ساعت 12:36

بسیار عالی