//Define a method that detect change in fooditemtype combo box and display all the food with that food type on the grid box private void cboChangeFoodItemType_SelectedIndexChange(object sender, EventArgs e) { //if resetting combo, ignore... if (cboChangeFoodItemType.SelectedIndex == -1) { return; } //Retrieve the food type and store the food type inside a variable and pass into the SQL //MessageBox.Show("Change Detected!"); 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); //remove previous item in the gridview //for(int r = 0 ; r < this.grdChangeFoodItemSelectFoodItem.Rows.Count ; r++) //{ // grd //} //load data from ds to grpChangeFoodItemSelectFood if (DS != null) { //grdChangeFoodItemSelectFoodItem.AutoGenerateColumns = true; //grdChangeFoodItemSelectFoodItem.DataSource = DS; grdChangeFoodItemSelectFoodItem.DataSource = DS.Tables["FoodItems"]; /*int row = DS.Tables["FoodItems"].Rows.Count - 1; * for(int r = 0; r <= row; r++) * { * grdChangeFoodItemSelectFoodItem.Rows.Add(); * grdChangeFoodItemSelectFoodItem.Rows[r].Cells[0].Value = DS.Tables["FoodItems"].Rows[r].ItemArray[0]; * grdChangeFoodItemSelectFoodItem.Rows[r].Cells[1].Value = DS.Tables["FoodItems"].Rows[r].ItemArray[1]; * grdChangeFoodItemSelectFoodItem.Rows[r].Cells[2].Value = DS.Tables["FoodItems"].Rows[r].ItemArray[2]; * grdChangeFoodItemSelectFoodItem.Rows[r].Cells[3].Value = DS.Tables["FoodItems"].Rows[r].ItemArray[3]; * grdChangeFoodItemSelectFoodItem.Rows[r].Cells[4].Value = DS.Tables["FoodItems"].Rows[r].ItemArray[4]; * }*/ } cboChangeFoodItemFoodType.SelectedIndex = cboChangeFoodItemType.SelectedIndex; }
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"]; } } }