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; } }
//Define a method that will extract the food type and store into the foodtype variable in this class private void cboAddFoodItemType_SelectedIndexChange(object sender, EventArgs e) { //if resetting combo, ignore... if (cboAddFoodItemType.SelectedIndex == 1) { return; } //find food type details , create a new fooditems and store the food type details into food item FoodItems newFoodItem = new FoodItems(); newFoodItem.setFoodType(cboAddFoodItemType.Text.Substring(0, 1)); //Validation to prevent the food type is empty if (newFoodItem.getFoodType() == "") { MessageBox.Show("No food type selected", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); cboAddFoodItemType.Focus(); return; } }