public static double getMealCalorie(String day, int PlanID, String MealType) { DataTable MealDetail = new DataTable("MealDetail"); MealDetail = getMealDetail(PlanID, day, MealType); double totalCalorie = 0; decimal foodCalorie = 0; decimal quantity = 0; double weight = 0; double unit = 0; for (int i = 0; i < MealDetail.Rows.Count; i++) { foodCalorie = (decimal)MealDetail.Rows[i]["FoodCalorie"]; unit = CatalogAccess.getFoodUnit((String)MealDetail.Rows[i]["FoodName"]); // decimal.TryParse((String)MealDetail.Rows[i]["Quantity"], out quantity); if (!String.IsNullOrEmpty(MealDetail.Rows[i]["Quantity"].ToString())) { quantity = (System.Decimal)MealDetail.Rows[i]["Quantity"]; } else if (!String.IsNullOrEmpty(MealDetail.Rows[i]["Weight"].ToString())) { weight = Convert.ToDouble((System.Decimal)MealDetail.Rows[i]["Weight"]); } // weight = 1; if (quantity != 0) { totalCalorie += Convert.ToDouble((decimal)foodCalorie * quantity); } else if (weight != 0) { totalCalorie += ((double)foodCalorie * weight) / unit; } } return(totalCalorie); }
//for gridview with selection function public static double getGridViewSumCalorieWithSelection(GridView GridView1) { decimal foodCalorie = 0; int quantity = 0; decimal weight = 0; double totalCalorie = 0; double unit = 0; for (int i = 0; i < GridView1.Rows.Count; i++) { if (decimal.TryParse(GridView1.Rows[i].Cells[4].Text, out foodCalorie) && int.TryParse(GridView1.Rows[i].Cells[2].Text, out quantity)) { totalCalorie += (double)foodCalorie * quantity; } else if (decimal.TryParse(GridView1.Rows[i].Cells[4].Text, out foodCalorie) && decimal.TryParse(GridView1.Rows[i].Cells[3].Text, out weight)) { unit = CatalogAccess.getFoodUnit(GridView1.Rows[i].Cells[1].Text); totalCalorie += (double)(foodCalorie * weight) / unit; } } return(totalCalorie); }