מחיקת קטגוריה

הסרטון

הקוד

Delete Category Win

clip_image002

 

using System;

using
System.Collections.Generic;

using
System.ComponentModel;

using
System.Data;

using
System.Drawing;

using
System.Linq;

using
System.Text;

using
System.Threading.Tasks;

using System.Windows.Forms;

 

namespace exampleProj.Forms

{

    public partial class DeleteCategoryWin : Form

    {

        dbEntities db = new dbEntities();

        List<productView> prodList;

        List<categoryTBL> catList;

        public DeleteCategoryWin()

        {

            InitializeComponent();

        }

        private void
DeleteCategoryWin_Load(
object sender,
EventArgs e)

        {

            catList = new List<categoryTBL>();

            updateAll();

        }

        private void
updateAll()

        {

            catList = (from s in db.categoryTBL orderby s.name select
s).ToList();

            dgvCategory.DataSource =
cbxCategory.DataSource = catList;

            cbxCategory.DisplayMember = "name";

            cbxCategory.ValueMember = "Id";

            btnDeleteCat.Enabled =
catList.Count > 0;

            lblCountCat.Text = "מספר הקטגוריות הוא : " + catList.Count;

        }

        private void
cbxCategory_SelectedIndexChanged(
object sender, EventArgs e)

        {

            categoryTBL c1 =
(categoryTBL)cbxCategory.SelectedItem;

            if (c1 == null)

                return;

            prodList = (from s in db.productView where s.catID == c1.Id select s).ToList();

            dgvProduct.DataSource = prodList;

            lblCountProd.Text = "מס המוצרים בקטגוריה הוא : " + prodList.Count;

        }

        private void
btnDeleteCat_Click(
object sender,
EventArgs e)

        {

            categoryTBL c1 =
(categoryTBL)cbxCategory.SelectedItem;

            if (c1 == null)

                return;

            prodList = (from s in db.productView where s.catID == c1.Id select s).ToList();

            if (prodList.Count > 0)

            {

                MessageBox.Show("לא ניתן למחוק קטגוריה שיש בה פריטים");

                return;

            }

            categoryTBL c2 = (from s in db.categoryTBL where s.Id == c1.Id select s).FirstOrDefault();

            db.categoryTBL.Remove(c2);

            db.SaveChanges();

            updateAll();

        }

        private void
DeleteCategoryWin_FormClosing(
object sender, FormClosingEventArgs e)

        {

            db.Dispose();

        }

    }

}