1

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?

Toppopia
  • 57
  • 1
  • 4

4 Answers4

0

You can make a comboBox and set this event, next, with Datagridview1.Row.Add method insert a row in Datagridview. But before it, you should a DatagridviewRow with some cells, that own cell of this row is DataGrifViewComboBoxCell.

Good Luck...

a d
  • 552
  • 2
  • 7
  • 17
0

On the form load event

ingredientList_  = context.Ingredients.OrderBy(p => p.ingredientName).ToList();

FillCombo();

//Create a method to fill combo

private void FillCombo()
  {
    IngredientBindingSource.DataSource = ingredientList_;
  }
vacano
  • 1
  • 2
0

In order to put combo box to third field you have to create first two as a datagridview items:

  DataGridViewRow RowSample = new DataGridViewRow();
  DataGridViewComboBoxCell  pizzaItem = new DataGridViewComboBoxCell();
  pizzaItem.DataSource = pizzaList_;  
  pizzaItem.Value = pizzaList_[0];
  DataGridViewCell pizzaName = new DataGridViewTextBoxCell();
  pizzaName.Value = pizza.pizzaName; // creating the text cell
  DataGridViewCell pizzaPrice = new DataGridViewTextBoxCell();
  pizzaPrice.Value = pizza.pizzaPrice;; // creating the text cell
  RowSample.Cells.Add(pizzaName);
  RowSample.Cells.Add(pizzaPrice);
  RowSample.Cells.Add(pizzaItem); 
  SampleGridView.Rows.Add(RowSample);

Now RowSample added to you datagridview with 3 field, and the third one is combobox.

inside
  • 3,047
  • 10
  • 49
  • 75
0

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 Ingredients class and click Finish.

  • Remeber to set the 1st ComboBox columns DataMember to ingredientList, you will need to select the IngredientsDataBindingSource control that was added (slightly below the Form Design's surface - in the gray area)

  • 2nd and 3rd add two TextBox columns for pizzaName and retailPrice and set there DataPropertyName accordingly.

 pizzaList_ = new BindingList<Pizza>();
 //Insert code to populate the List of Pizza's and Ingredients
 dataGridViewPizzaMenu.AutoGenerateColumns = false;
 dataGridViewPizzaMenu.DataSource = pizzaList_;
 ingredientsDataBindingSource.DataSource = pizzaList_.ingredientsList_;    

ps There is a downloadable sample in the link I referred to above.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I'm a bit confused on what "//ingredientsDataBindingSource.DataSource = pizzaList_.ingredientsList_;" is meant to be. After following those steps, I now have the column binded to the ingredients object. My previous attempt to add the values to that column were this: List ingredientList1 = new List(); ingredientList1.Add(bambooShoots); ingredientList1.Add(banana); ingredientList1.Add(capsicum); Pizza pizza1 = new Pizza(ingredientList1, "Odd Pizza", 5.99m); pizzaList_.Add(pizza1); – Toppopia Jan 26 '13 at 02:26
  • (Continued) the data grid shows the name and price, but the combo box appears empty. So i know i have to change something, but i don't know what. The ingredients are created up further in the code. – Toppopia Jan 26 '13 at 02:28
  • did you see the steps in the answer I linked too? http://stackoverflow.com/a/13772751/495455 maybe you need to specify the `DataPropertyName` to ingredientList_ with the underscore at the end – Jeremy Thompson Jan 26 '13 at 03:04
  • I have followed the steps but i can't set the DataPropertyName as ingredientList_ the only options that show up are the pizzaName and pizzaPrice. Also, i can't use the IngredientsDataBindingSource.DataSource code because it says that "IngredientsDataBindingSource.DataSource does not exist in the current context". – Toppopia Jan 26 '13 at 05:03
  • I think I understand. You mean the only two are pizzaName and retailPrice (or pizzaPrice). Ok so you successfully setup the two textbox columns with DataPRopertyName's that match the Data structure (thats how the mapping is done automatically when binding). Now click on the IngredientsDataBindingSource - once you create it for the 1st combobox column, it will show up below Form in design view. You need to select the IngredientsDataBindingSource control to see its properties and set its `DataMember` to ingredientList_ – Jeremy Thompson Jan 26 '13 at 10:27