הסרטון
הקוד
Modify Category Win
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 ModifyCategoryWin : Form
{
dbEntities db = new dbEntities();
List<categoryTBL> catList;
public ModifyCategoryWin()
{
InitializeComponent();
}
private void
ModifyCategoryWin_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";
btnUpdateCat.Enabled =
catList.Count > 0;
lblCountCat.Text = "מספר הקטגוריות הוא : " + catList.Count;
}
private void
btnUpdateCat_Click(object sender,
EventArgs e)
{
categoryTBL c1 =
(categoryTBL)cbxCategory.SelectedItem;
if (c1 == null)
return;
categoryTBL c2 = (from s in db.categoryTBL where s.Id == c1.Id select s).FirstOrDefault();
int idx = cbxCategory.SelectedIndex;
// יש להוסיף בדיקות תקינות
c2.name = txbCatName.Text.Trim();
c2.description =
txbCatDesc.Text.Trim();
db.SaveChanges();
updateAll();
cbxCategory.SelectedIndex = idx;
}
private void
cbxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
categoryTBL c1 =
(categoryTBL)cbxCategory.SelectedItem;
if (c1 == null)
return;
txbCatName.Text = c1.name;
txbCatDesc.Text = c1.description;
}
private void
ModifyCategoryWin_FormClosing(object sender, FormClosingEventArgs e)
{
db.Dispose();
}
}
}