private void btnChangeFoodItemSelectFoodItemButton_Click(object sender, EventArgs e) { if (grdChangeFoodItemSelectFoodItem.SelectedRows.Count < 0) { MessageBox.Show("Please select the food item to change"); } else if (grdChangeFoodItemSelectFoodItem.SelectedRows.Count > 1) { MessageBox.Show("Please select one food item to change only"); } else { //save the selected row into a food item object //first we have to get the row index from datagridview to retrieve the row int rowIndex = grdChangeFoodItemSelectFoodItem.SelectedCells[0].RowIndex; DataGridViewRow selectedRow = grdChangeFoodItemSelectFoodItem.Rows[rowIndex]; //create fooditem object based on the selected row FoodItems selectedItem = new FoodItems(Convert.ToInt32(selectedRow.Cells[0].Value), Convert.ToString(selectedRow.Cells[1].Value), Convert.ToString(selectedRow.Cells[2].Value), Convert.ToString(selectedRow.Cells[3].Value), Convert.ToDecimal(selectedRow.Cells[4].Value), Convert.ToString(selectedRow.Cells[5].Value)); //set all the text to the corresponding food items variable this.txtChangeFoodItemId.Text = Convert.ToString(selectedItem.getItemId()); this.txtChangeFoodItem.Text = selectedItem.getItemName(); this.txtChangeFoodItemDesc.Text = selectedItem.getDescription(); this.cboChangeFoodItemType.Text = selectedItem.getFoodType(); this.txtChgFoodItemPrice.Value = selectedItem.getPrice(); //this.cboChangeFoodItemFoodStatus.Text = selectedItem.getStatus(); rtnStatus(selectedItem.getStatus()); //display change food item group grpChangeFoodItemSelectFood.Visible = false; grpChangeFoodItemChangeFood.Visible = true; } }
private void btnChgFoodItemSubmit_Click(object sender, EventArgs e) { bool allCorrect = true; //validate data if (txtChangeFoodItem.Text.Equals("")) { MessageBox.Show("Food Item name must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); allCorrect = false; } else if (txtChangeFoodItemDesc.Text.Equals("")) { MessageBox.Show("Food item description must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); allCorrect = false; } /*else if(txtChangeFoodItemDesc.Text.Length > 15) * { * MessageBox.Show("Food item name must shorter than 15", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); * allCorrect = false; * }*/ else if (cboChangeFoodItemFoodType.SelectedItem == null) { MessageBox.Show("Food item type must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); allCorrect = false; } else if (txtChgFoodItemPrice.Text.Equals("")) { MessageBox.Show("Food item price must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); allCorrect = false; } else if (cboChangeFoodItemFoodStatus.SelectedItem == null) { MessageBox.Show("Food item status must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); allCorrect = false; } else { //Price value must be greater than 0 Match validPrice1 = Regex.Match(txtChgFoodItemPrice.Text, @"\d\d\.\d\d"); Match validPrice2 = Regex.Match(txtChgFoodItemPrice.Text, @"\d"); Match validPrice3 = Regex.Match(txtChgFoodItemPrice.Text, @"\d\d"); Match validPrice4 = Regex.Match(txtChgFoodItemPrice.Text, @"\d.\d\d"); if (!validPrice1.Success && !validPrice2.Success && !validPrice3.Success && !validPrice4.Success) { MessageBox.Show("Food item price must be in decimal form and greater than 0"); allCorrect = false; } } if (allCorrect == true) { //save every details from the detail box into a food item FoodItems foodItem = new FoodItems(Convert.ToInt32(txtChangeFoodItemId.Text), txtChangeFoodItem.Text, txtChangeFoodItemDesc.Text, cboChangeFoodItemFoodType.Text.Substring(0, 1), Convert.ToDecimal(txtChgFoodItemPrice.Value), cboChangeFoodItemFoodStatus.Text.Substring(0, 1)); //Insert fooditem via written SQL method foodItem.updateFoodItem(); //show successful message MessageBox.Show("Food item changed successfully!"); //make sure the datasource output is updated too DataSet DS = new DataSet(); DS = FoodItems.getSelectedFoodItem(DS, foodItem.getStatus()); if (DS != null) { if (DS.Tables[0].Rows.Count != 0) { grdChangeFoodItemSelectFoodItem.DataSource = DS.Tables["FoodItems"]; } } //clear everything txtChangeFoodItem.Clear(); txtChangeFoodItemDesc.Clear(); txtChgFoodItemPrice.Value = 0; cboChangeFoodItemFoodStatus.SelectedIndex = -1; //cboChangeFoodItemFoodType.SelectedIndex = -1; grpChangeFoodItemSelectFood.Visible = true; grpChangeFoodItemChangeFood.Visible = false; string foodType = cboChangeFoodItemType.Text.Substring(0, 1); //Get the dataset by passing the foodtype as parameter to retrieve selectedFoodItem dataset DataSet ds = new DataSet(); ds = FoodItems.getSelectedFoodItem(ds, foodType); //load data from ds to grpChangeFoodItemSelectFood if (ds != null) { grdChangeFoodItemSelectFoodItem.DataSource = ds.Tables["FoodItems"]; } } }