//삭제버튼 private void simpleButton5_Click(object sender, EventArgs e) { if (dataGridView1.RowCount < 1) { MessageBox.Show("삭제할 항목이 없습니다.", "에러", MessageBoxButtons.OK); return; } int ID = (int)dataGridView1.SelectedCells[0].Value; using (DeskAssemblyEntities db = new DeskAssemblyEntities()) { try { Item item = db.Items.Find(ID); db.Items.Remove(item); db.SaveChanges(); MessageBox.Show("삭제 완료", "삭제", MessageBoxButtons.OK); itemBindingSource.RemoveCurrent(); } catch (Exception ex) { MessageBox.Show("삭제할 항목이 없습니다."); } } }
//등록버튼 private void simpleButton3_Click(object sender, EventArgs e) { using (DeskAssemblyEntities db = new DeskAssemblyEntities()) { if (textBox1.Text == "" && textBox2.Text == "") { return; } Item item = new Item(); item.Name = textBox1.Text; item.Price = int.Parse(textBox2.Text); if (comboBoxEdit1.Text == "부품") { item.IsProduct = false; } else if (comboBoxEdit1.Text == "제품") { item.IsProduct = true; } else { return; } item.Image = ConvertImageToByte(pictureBox1.Image); db.Items.Add(item); db.SaveChanges(); if (item.IsProduct == false) { MessageBox.Show("부품 등록 완료", "Message", MessageBoxButtons.OKCancel); } else { MessageBox.Show("제품 등록 완료", "Message", MessageBoxButtons.OKCancel); } itemBindingSource.Add(item); } textBox1.Text = ""; textBox2.Text = ""; textBox4.Text = ""; pictureBox1.Image = null; }
//수정버튼 private void simpleButton4_Click(object sender, EventArgs e) { int ID = (int)dataGridView1.SelectedCells[0].Value; using (DeskAssemblyEntities db = new DeskAssemblyEntities()) { if (textBox1.Text == "" && textBox2.Text == "") { return; } Item item = db.Items.Find(ID); if (item != null) { item.Name = textBox1.Text; item.Price = int.Parse(textBox2.Text); if (comboBoxEdit1.Text == "부품") { item.IsProduct = false; } else if (comboBoxEdit1.Text == "제품") { item.IsProduct = true; } else { return; } item.Image = ConvertImageToByte(pictureBox1.Image); db.SaveChanges(); itemBindingSource.DataSource = db.Items.ToList(); } } }