//delete buttun: delete any product in DB private void btn_delete_Click(object sender, EventArgs e) { try { if (_Id != -1) { using (MarketDbContext marketDbContext = new MarketDbContext()) { Model.TopSellingStation _TopSellingStation = marketDbContext.TopSellingStations.Where(x => x.Id == _Id).FirstOrDefault(); marketDbContext.TopSellingStations.Remove(_TopSellingStation); marketDbContext.SaveChanges(); dataGrid_allProduct.DataSource = marketDbContext.TopSellingStations.ToList(); _Id = -1; } //on double click 'add button' will visibility=false. So must be now visibility=true btn_add.Enabled = true; } } catch (Exception exp) { MessageBox.Show(exp.Message); } finally { Clear(); } }
//update button: select current products and change any information private void btn_update_Click(object sender, EventArgs e) { try { if (_Id != -1) { using (MarketDbContext marketDbContext = new MarketDbContext()) { Model.TopSellingStation _TopSellingStation = marketDbContext.TopSellingStations.Where(x => x.Id == _Id).FirstOrDefault(); _TopSellingStation.Name = txt_Name.Text; _TopSellingStation.Category = txt_Category.Text; _TopSellingStation.Price = Convert.ToDecimal(txt_Price.Text == "" ? null : txt_Price.Text); _TopSellingStation.Count = Convert.ToInt16(txt_Count.Text == "" ? null : txt_Count.Text); _TopSellingStation.SellingTime = Convert.ToDateTime(date_time.Text); _TopSellingStation.Currency = (Currency)cmbx_Currency.SelectedIndex; _TopSellingStation.Usage = Convert.ToBoolean(cmbx_Usage.Text); _TopSellingStation.Weight = (Weight)cmbx_Weight.SelectedIndex; marketDbContext.SaveChanges(); dataGrid_allProduct.DataSource = marketDbContext.TopSellingStations.ToList(); _Id = -1; } } } catch (Exception exp) { MessageBox.Show(exp.Message); } finally { Clear(); btn_add.Enabled = true; } }
//when double click to datagridview so set information again txtbox and so on private void dataGrid_allProduct_DoubleClick(object sender, EventArgs e) { if (dataGrid_allProduct.CurrentRow.Index != -1) { _Id = (int)dataGrid_allProduct.CurrentRow.Cells["Id"].Value; using (MarketDbContext marketDbContext = new MarketDbContext()) { Model.TopSellingStation _TopSellingStation = marketDbContext.TopSellingStations .Where(x => x.Id == _Id) .FirstOrDefault(); txt_Name.Text = _TopSellingStation.Name; txt_Category.Text = _TopSellingStation.Category; txt_Count.Text = _TopSellingStation.Count.ToString(); txt_Price.Text = _TopSellingStation.Price.ToString(); } btn_add.Enabled = false; } ; }
private void btn_add_Click(object sender, EventArgs e) { try { //add all product information from User Model.TopSellingStation topSellingStation = new Model.TopSellingStation() { Name = txt_Name.Text, Category = txt_Category.Text, Price = Convert.ToDecimal(txt_Price.Text == ""?null:txt_Price.Text), Count = Convert.ToInt16(txt_Count.Text == ""?null:txt_Count.Text), SellingTime = Convert.ToDateTime(date_time.Text), Currency = (Currency)cmbx_Currency.SelectedIndex, Usage = Convert.ToBoolean(cmbx_Usage.Text), Weight = (Weight)cmbx_Weight.SelectedIndex }; //VALIDATE #region Validate ValidationContext context = new ValidationContext(topSellingStation, null, null); List <ValidationResult> errors = new List <ValidationResult>(); if (!Validator.TryValidateObject(topSellingStation, context, errors, true)) { foreach (ValidationResult result in errors) { if (result.MemberNames.Contains("Name")) { errorProvider_Name.SetError(txt_Name, result.ErrorMessage); } else if (result.MemberNames.Contains("Category")) { errorProvider_Name.SetError(txt_Category, result.ErrorMessage); } else if (result.MemberNames.Contains("Price")) { errorProvider_Name.SetError(txt_Price, result.ErrorMessage); } else if (result.MemberNames.Contains("Count")) { errorProvider_Name.SetError(txt_Count, result.ErrorMessage); } else { Clear(); } } } #endregion else { //save all product to DB using (MarketDbContext marketDbContext = new MarketDbContext()) { marketDbContext.TopSellingStations.Add(topSellingStation); marketDbContext.SaveChanges(); dataGrid_allProduct.DataSource = marketDbContext.TopSellingStations.ToList(); } Clear(); } } catch (Exception exp) { MessageBox.Show(exp.Message); } }