עדכון מוצר

הסרטון

הקוד



Modify Product Win

clip_image002[4]

 

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 ModifyProductWin : Form

    {

        dbEntities db = new dbEntities();

        List<categoryTBL> catList;

        List<productView> prodList;

        public ModifyProductWin()

        {

            InitializeComponent();

        }

        private void
ModifyProductWin_Load(
object sender,
EventArgs e)

        {

            prodList = new List<productView>();

            updateAll();

        }

        private void
updateAll()

        {

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

            prodList = (from s in db.productView orderby s.productName select s).ToList();

            cbxCategory.DataSource = catList;

            cbxCategory.DisplayMember = "name";

            cbxCategory.ValueMember = "Id";

            dgvProduct.DataSource =
cbxProduct.DataSource = prodList;

            cbxProduct.DisplayMember = "productName";

            cbxProduct.ValueMember = "Id";

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

            btnUpdateProduct.Enabled =
prodList.Count > 0;

        }

        private void
cbxProduct_SelectedIndexChanged(
object sender, EventArgs e)

        {

            productView p1 =
(productView)cbxProduct.SelectedItem;

            if (p1 == null)

                return;

            txbProdName.Text = p1.productName;

            txbProdPrice.Text = p1.price + "";

            categoryTBL c1 = catList.Where(x
=> x.Id == p1.catID).FirstOrDefault();

            cbxCategory.SelectedItem = c1;

        }

        private void
btnUpdateProduct_Click(
object sender,
EventArgs e)

        {

            productView p1 =
(productView)cbxProduct.SelectedItem;

            if (p1 == null)

                return;

            // יש לבצע אימות נתונים

            int idx = cbxProduct.SelectedIndex;

            string pName = txbProdName.Text.Trim();

            string pPrice = txbProdPrice.Text.Trim();

            categoryTBL c1 =
(categoryTBL)cbxCategory.SelectedItem;

            productTBL prodToUpdate = (from s in db.productTBL where s.Id == p1.Id select s).FirstOrDefault();

            prodToUpdate.name = pName;

            prodToUpdate.price = (float)Convert.ToDouble(pPrice);

            prodToUpdate.catID = c1.Id;

            db.SaveChanges();

            updateAll();

            cbxProduct.SelectedIndex = idx;

        }

        private void
ModifyProductWin_FormClosing(
object sender, FormClosingEventArgs e)

        {

            db.Dispose();

        }

    }

}