private void UpdateBikeForm_Load(object sender, EventArgs e) { //custom data format dtpManufactured.Format = DateTimePickerFormat.Custom; dtpManufactured.CustomFormat = "yyyy"; dtpManufactured.ShowUpDown = true; //load model data using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var models = from m in kiwiBike4RentEntities.MODELs select m; foreach (MODEL m in models.ToList()) { listModels.Items.Add("Model ID: " + m.ModelID + " , Brand: " + m.Brand + ", Category:" + m.Category + ", Weight: " + m.Weight + ", Description: " + m.Description + ", Dimension: " + m.Dimension + ", Deposit: " + m.Deposit + ", Rate: " + m.Rate); } //set form values according to previews form passed bikeID var bike = kiwiBike4RentEntities.BIKEs.Where(b => b.BikeID == bikeID).SingleOrDefault(); dtpManufactured.Value = bike.ManufacturedYear; txtLifeTime.Text = bike.LifeTime.ToString(); txtRentalPrice.Text = bike.RentalPrice.ToString(); lboxStatus.SelectedIndex = lboxStatus.FindString(bike.Status); lboxAccessories.SelectedIndex = lboxAccessories.FindString(bike.Accessories); string selectedModel = ""; foreach (string m in listModels.Items) { if (m.Split(',')[0].Split(':')[1].Trim().Equals(bike.Model.ToString())) { selectedModel = m; break; } } listModels.SelectedIndex = listModels.FindString(selectedModel); } }
private void btnSearch_Click(object sender, EventArgs e) { //load data with related table(model1) and re-orgnize data structure using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var bikes = from allBikes in kiwiBike4RentEntities.BIKEs.Include("MODEL1") select new { allBikes.BikeID, Model = allBikes.MODEL1.ModelID, allBikes.MODEL1.Brand, allBikes.MODEL1.Category, allBikes.Status, allBikes.RentalPrice, allBikes.MODEL1.Dimension, allBikes.MODEL1.Weight, allBikes.MODEL1.Description, allBikes.MODEL1.Deposit, allBikes.MODEL1.Rate, allBikes.Accessories, allBikes.LifeTime, allBikes.ManufacturedYear, allBikes.rentedDate, allBikes.dueBackDate }; //according to inputs to filter result if (!string.IsNullOrEmpty(boxCategory.Text)) { bikes = bikes.Where(b => b.Category == boxCategory.Text); } if (!string.IsNullOrEmpty(boxModel.Text)) { Int32.TryParse(boxModel.Text, out int i); bikes = bikes.Where(b => b.Model == i); } if (!string.IsNullOrEmpty(boxBrand.Text)) { bikes = bikes.Where(b => b.Brand == boxBrand.Text); } if (!string.IsNullOrEmpty(boxStatus.Text)) { bikes = bikes.Where(b => b.Status == boxStatus.Text); } if (!string.IsNullOrEmpty(txtRentalPrice.Text)) { int i = 0; Int32.TryParse(txtRentalPrice.Text, out i); if (i == 0) { MessageBox.Show("Rental price must be a number"); txtRentalPrice.Focus(); return; } bikes = bikes.Where(b => b.RentalPrice < i); } //put data to dataGridview dgvManageBikes.DataSource = bikes.ToList(); } }
public void FreshBikes() { //load data with related table(model1) and re-orgnize data structure using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var bikes = from allBikes in kiwiBike4RentEntities.BIKEs.Include("MODEL1") select new { allBikes.BikeID, Model = allBikes.MODEL1.ModelID, allBikes.MODEL1.Brand, allBikes.MODEL1.Category, allBikes.Status, allBikes.RentalPrice, allBikes.MODEL1.Dimension, allBikes.MODEL1.Weight, allBikes.MODEL1.Description, allBikes.MODEL1.Deposit, allBikes.MODEL1.Rate, allBikes.Accessories, allBikes.LifeTime, allBikes.ManufacturedYear, allBikes.rentedDate, allBikes.dueBackDate }; dgvManageBikes.DataSource = bikes.ToList(); } }
private void FreshBikeModel() { listModels.Items.Clear(); using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var models = from m in kiwiBike4RentEntities.MODELs select m; foreach (MODEL m in models.ToList()) { listModels.Items.Add("Model ID: " + m.ModelID + " , Brand: " + m.Brand + ", Category:" + m.Category + ", Weight: " + m.Weight + ", Description: " + m.Description + ", Dimension: " + m.Dimension + ", Deposit: " + m.Deposit + ", Rate: " + m.Rate); } } }
private void AddBikeForm_Load(object sender, EventArgs e) { //custom data format dtpManufactured.Format = DateTimePickerFormat.Custom; dtpManufactured.CustomFormat = "yyyy"; dtpManufactured.ShowUpDown = true; //load model data using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var models = from m in kiwiBike4RentEntities.MODELs select m; foreach (MODEL m in models.ToList()) { listModels.Items.Add("Model ID: " + m.ModelID + " , Brand: " + m.Brand + ", Category:" + m.Category + ", Weight: " + m.Weight + ", Description: " + m.Description + ", Dimension: " + m.Dimension + ", Deposit: " + m.Deposit + ", Rate: " + m.Rate); } } }
private void UpdateBikesForm_Load(object sender, EventArgs e) { //load data with related table(model1) and re-orgnize data structure using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var brand = kiwiBike4RentEntities.MODELs.Select(b => b.Brand).Distinct(); foreach (string b in brand.ToList()) { boxBrand.Items.Add(b); } var category = kiwiBike4RentEntities.MODELs.Select(c => c.Category).Distinct(); foreach (string c in category.ToList()) { boxCategory.Items.Add(c); } var model = kiwiBike4RentEntities.MODELs.Select(m => m.ModelID).Distinct(); foreach (int m in model) { boxModel.Items.Add(m); } var bikes = from allBikes in kiwiBike4RentEntities.BIKEs.Include("MODEL1") select new { allBikes.BikeID, Model = allBikes.MODEL1.ModelID, allBikes.MODEL1.Brand, allBikes.MODEL1.Category, allBikes.Status, allBikes.RentalPrice, allBikes.MODEL1.Dimension, allBikes.MODEL1.Weight, allBikes.MODEL1.Description, allBikes.MODEL1.Deposit, allBikes.MODEL1.Rate, allBikes.Accessories, allBikes.LifeTime, allBikes.ManufacturedYear, allBikes.rentedDate, allBikes.dueBackDate }; dgvManageBikes.DataSource = bikes.ToList(); } }
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 ViewBikesForm_Load(object sender, EventArgs e) { //initial combobox data using (KiwiBike4RentEntities kiwiBike4RentEntities = new KiwiBike4RentEntities()) { var brand = kiwiBike4RentEntities.MODELs.Select(b => b.Brand).Distinct(); foreach (string b in brand.ToList()) { brandBox.Items.Add(b); } var category = kiwiBike4RentEntities.MODELs.Select(c => c.Category).Distinct(); foreach (string c in category.ToList()) { categoryBox.Items.Add(c); } var model = kiwiBike4RentEntities.MODELs.Select(m => m.ModelID).Distinct(); foreach (int m in model) { modelBox.Items.Add(m); } } }
private void button1_Click(object sender, EventArgs e) { // data check if (string.IsNullOrEmpty(textBrand.Text)) { MessageBox.Show("Brand can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBrand.Focus(); return; } if (string.IsNullOrEmpty(categoryBox.Text)) { MessageBox.Show("Category can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); categoryBox.Focus(); return; } if (!Constants.categories.Contains(categoryBox.Text)) { MessageBox.Show("Please choose a category in existed list!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); categoryBox.Focus(); return; } if (string.IsNullOrEmpty(textWeight.Text)) { MessageBox.Show("Weight can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textWeight.Focus(); return; } if (string.IsNullOrEmpty(textDimension.Text)) { MessageBox.Show("Dimension can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textDimension.Focus(); return; } if (string.IsNullOrEmpty(textDeposit.Text)) { MessageBox.Show("Deposit can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textDeposit.Focus(); return; } if (string.IsNullOrEmpty(textRate.Text)) { MessageBox.Show("Rate can not be empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textRate.Focus(); return; } //insert data MODEL model = new MODEL(); model.Brand = textBrand.Text; model.Category = categoryBox.Text; model.Description = descriptionBox.Text; model.Dimension = textDimension.Text; float weight = 0; float.TryParse(textWeight.Text, out weight); if (weight <= 0) { MessageBox.Show("Weight time has to be a number and > 0!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textWeight.Focus(); return; } model.Weight = weight; int deposit = 0; Int32.TryParse(textDeposit.Text, out deposit); if (deposit <= 0) { MessageBox.Show("Deposit has to be a number and > 0!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textDeposit.Focus(); return; } model.Deposit = deposit; int rate = 0; Int32.TryParse(textRate.Text, out rate); if (rate < 10 || rate > 50) { MessageBox.Show("Rate has to be a number in range between 10 to 50 !", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textRate.Focus(); return; } model.Rate = rate; using (KiwiBike4RentEntities kiwi = new KiwiBike4RentEntities()) { kiwi.MODELs.Add(model); kiwi.SaveChanges(); } MessageBox.Show("Bike Model Added!", "Model", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }
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(); }