private void btnDelete_Click(object sender, EventArgs e) { if (dgvManageBikes.SelectedRows.Count <= 0) { MessageBox.Show("Please Select at least one row for deleteing action!!", "Delete Action", MessageBoxButtons.OK, MessageBoxIcon.Information); } foreach (DataGridViewRow row in dgvManageBikes.SelectedRows) { StringBuilder bikeTempInfo = new StringBuilder(); foreach (DataGridViewCell c in row.Cells) { bikeTempInfo.Append(c.Value).Append(", "); } DialogResult result = MessageBox.Show("This Bike will be deleted!!!\n" + bikeTempInfo.ToString(), "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (DialogResult.Yes == result) { //delete using (KiwiBike4RentEntities kiwi = new KiwiBike4RentEntities()) { Int32.TryParse(row.Cells[0].Value.ToString(), out int bikeID); BIKE bike = new BIKE { BikeID = bikeID }; kiwi.BIKEs.Attach(bike); kiwi.BIKEs.Remove(bike); kiwi.SaveChanges(); } continue; } if (DialogResult.No == result) { continue; } if (DialogResult.Cancel == result) { break; } } FreshBikes(); }
private void btnSave_Click(object sender, EventArgs e) { // data check string modelText = listModels.GetItemText(listModels.SelectedItem); if (string.IsNullOrEmpty(modelText)) { MessageBox.Show("Bike model can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); listModels.Focus(); return; } if (string.IsNullOrEmpty(lboxStatus.Text)) { MessageBox.Show("Status can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); lboxStatus.Focus(); return; } if (!Constants.status.Contains(lboxStatus.Text)) { MessageBox.Show("Please choose a status in existed list!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); lboxStatus.Focus(); return; } if (string.IsNullOrEmpty(dtpManufactured.Text)) { MessageBox.Show("Manufactory Time can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); dtpManufactured.Focus(); return; } if (string.IsNullOrEmpty(txtLifeTime.Text)) { MessageBox.Show("Life Time can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtLifeTime.Focus(); return; } if (string.IsNullOrEmpty(txtRentalPrice.Text)) { MessageBox.Show("Rental Price can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtRentalPrice.Focus(); return; } if (string.IsNullOrEmpty(lboxAccessories.Text)) { MessageBox.Show("Accessory can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); lboxAccessories.Focus(); return; } if (!Constants.asscessories.Contains(lboxAccessories.Text)) { MessageBox.Show("Please choose a asscessory in existed list!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); lboxAccessories.Focus(); return; } //insert data BIKE bike = new BIKE(); int modelID = 0; Int32.TryParse(modelText.Split(',')[0].Split(':')[1], out modelID); bike.Model = modelID; int lifeTime = 0; Int32.TryParse(txtLifeTime.Text, out lifeTime); if (lifeTime <= 0) { MessageBox.Show("life time has to be a number and bigger than 0!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtLifeTime.Focus(); return; } bike.LifeTime = lifeTime; int my = 0; Int32.TryParse(dtpManufactured.Text, out my); if (my <= 0 || my < 1960) { MessageBox.Show("Manufactured Year can not less than 1960", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); dtpManufactured.Focus(); return; } bike.ManufacturedYear = new DateTime(my, 1, 1); int rentalPrice = 0; Int32.TryParse(txtRentalPrice.Text, out rentalPrice); if (rentalPrice <= 0) { MessageBox.Show("Rental Price has to be a number and > 0 ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtRentalPrice.Focus(); return; } bike.RentalPrice = rentalPrice; bike.Status = lboxStatus.Text; bike.Accessories = lboxAccessories.Text; using (KiwiBike4RentEntities kiwi = new KiwiBike4RentEntities()) { kiwi.BIKEs.Add(bike); kiwi.SaveChanges(); } MessageBox.Show("Bike Added!", "Bike", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }