I am trying to make a program that has a data grid that shows on each row, List of Ingredients the pizza has, the pizza name, and the price of the pizza. I can get the data grid to show the name and price, but i am having trouble getting it to show the list of ingredients. The data grid’s datasource is a binding list of a class called Pizza.
class Pizza
{
private List<Ingredients> ingredientList_;
private string pizzaName_;
private decimal retailPrice_;
public Pizza(List<Ingredients> ingredientList, string pizzaName, decimal retailPrice)
{
ingredientList_ = ingredientList;
pizzaName_ = pizzaName;
retailPrice_ = retailPrice;
}
It has the basic get and set properties.
I also have an Ingredient class.
class Ingredients
{
private string name_;
private int servingSize_;
private int energyValue_;
private decimal purchasePrice_;
private bool isVegetarian_;
public Ingredients(string name, int servingSize, int energyValue, decimal purchasePrice, bool isVegetarian)
{
name_ = name;
servingSize_ = servingSize;
energyValue_ = energyValue;
purchasePrice_ = purchasePrice;
isVegetarian_ = isVegetarian;
}
Has the basic get and set properties.
In my form code, I have:
private BindingList<Pizza> pizzaList_;
pizzaList_ = new BindingList<Pizza>();
dataGridViewPizzaMenu.DataSource = pizzaList_;
Now my problem is that i am trying to use a combo box column to show the ingredients in a pizza when i click on it. But i can’t seem to create a bound column for the Ingredients, only the pizza name and pizza price. Am i missing something or is what i am trying to do not possible?
Everything you’ve done appears correct. The problem I think is the way the DataGridView is setup at Design or Runtime.
If you head over to this answer you can see the steps you need to take:
Add all elements of array to datagridview rows except one
The trick with binding the 1st combobox column is a BindingSource. In design time > right click on the DataGridView > choose Edit Columns > select the first column > choose DataSource > click Add Project DataSource > choose Object > then tick the
Ingredientsclass and click Finish.Remeber to set the 1st ComboBox columns
DataMemberto ingredientList, you will need to select the IngredientsDataBindingSource control that was added (slightly below the Form Design’s surface – in the gray area)DataPropertyNameaccordingly.ps There is a downloadable sample in the link I referred to above.