What you need is to create a
collection<type>, which would contain all of the food items, along with an additional property for food type. This collection could be populated with a database at sometime.
public class Food {
public int FoodID { get; set; }
public string FoodTyoe { get; set; }
public string FoodName { get; set; }
public Food() {}
public Food(int foodID, string foodType, string foodName) {
FoodID = foodID;
FoodType = foodType;
FoodName = foodName;
}
public Collection<Food> LoadAllFood() {
Collection<Food> foods = new Collection<Food>();
foods.Add(new Food(1, "Meat", "BBQ"));
foods.Add(new Food(2, "Fruit", "Pear"));
return foods;
}
}
So now you have all your foods. Now you need to
filter that down by the FoodType. This is easily achieved using a
lambda
expression in your Button method:
private void Button1_Click(object sender, EventArgs e) {
table.Rows.Clear();
Collection<Food> AllFoods = new Food().LoadAllFoods();
Collection<Food> SomeFoods = AllFoods.Where(f => f.Type == textBox1.Text);
foreach(Food item in SomeFoods) {
table.Rows.Add(item.FoodID, item.FoodName);
}
dataGridView1.Refresh();
}
Now this is hastily written and could be a lot more compact and efficient. I thought this would give you a start on how to use Object Orientated Programming.