Пример #1
0
        private void btnCreateOK_Click(object sender, EventArgs e)
        {
            if (txtFoodName.Text == "" || txtCity.Text == "" || txtDistrict.Text == "" || txtAddress.Text == "")
            {
                MessageBox.Show("必填資料未填完");
            }
            else
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                this.pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();

                Food food = new Food
                {
                    FoodName    = txtFoodName.Text,
                    City        = txtCity.Text,
                    District    = txtDistrict.Text,
                    Address     = txtAddress.Text,
                    StoreName   = txtStoreName.Text,
                    Picture     = bytes,
                    Phone       = txtPhone.Text,
                    Description = rtxtDescription.Text,
                    Remark      = rtxtRemark.Text
                };
                dbContext.Foods.Add(food);
                dbContext.SaveChanges();
                this.Close();

                foodForm.RefreshData(this.dbContext);
            }
        }
Пример #2
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (this.dataGridView1.DataSource == null)
     {
         MessageBox.Show("請先選取資料");
     }
     else
     {
         this.RefreshData(this.dbContext);
         var dataIndex = this.dataGridView1.SelectedCells[0].RowIndex;
         var data      = (Food)this.dataGridView1.Rows[dataIndex].DataBoundItem;
         this.dbContext.Foods.Remove(data);
         dbContext.SaveChanges();
         this.RefreshData(this.dbContext);
     }
 }