I'm trying to use DataGridView with a business object. Here's a simplified scenario: - call the object "Recipe", and it has a BindingList of a few Ingredient objects in the Recipe - I also have a "database", which is a BindingList of all available Ingredient objects
I would like to display a DataGridView of all the Ingredients in the Recipe, and allow the user to select new Ingredients from a combobox in the Name field.
When I try to set this up the way I think it should, the name of the Ingredients in the Recipe are not displayed, though I can select from the IngredientDB, and selecting a new Ingredient does update the list in the Recipe.
How do I get the name to display correctly?
Here's what I have so far:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1 {
public class Ingredient {
private String name;
public String Name {
get { return name; }
set { name = value; }
}
private string description;
public String Description {
get { return description; }
set { description = value; }
}
private int amount;
public int Amount {
get { return amount; }
set { amount = value; }
}
public Ingredient(String n, String d, int a) {
Name = n;
Description = d;
Amount = a;
}
public Ingredient() { }
}
public class Data {
BindingList<Ingredient> ingredientDB = null;
public BindingList<Ingredient> IngredientDB {
get { return ingredientDB; }
}
public Data() {
ingredientDB = new BindingList<Ingredient>();
ingredientDB.Add(new Ingredient("rice", "a grain", 2));
ingredientDB.Add(new Ingredient("whole wheat flour", "for baking", 1));
ingredientDB.Add(new Ingredient("butter", "fatty", 3));
}
}
public class Recipe : INotifyPropertyChanged {
public String Name {
get;
set;
}
BindingList<Ingredient> ingredients = null;
public BindingList<Ingredient> Ingredients {
get { return ingredients; }
set {
ingredients = value;
NotifyPropertyChanged("Ingredients");
}
}
public Recipe() {
Ingredients = new BindingList<Ingredient>();
Ingredients.Add(new Ingredient("Water", "Wet", 2));
Ingredients.Add(new Ingredient("Gin", "Yummy", 2));
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
public class myForm : Form {
private DataGridView dgv;
private BindingSource recipeBS;
private BindingSource dataBS;
public myForm() {
this.Height = 200;
this.Width = 400;
Recipe myRecipe = new Recipe();
Data myData = new Data();
recipeBS = new BindingSource(myRecipe, null);
dataBS = new BindingSource(myData, "IngredientDB");
dgv = new DataGridView();
dgv.Width = 400;
dgv.DataError += new
DataGridViewDataErrorEventHandler(DataGridView1_DataError);
dgv.DataSource = myRecipe.Ingredients;
// dgv.Columns.Remove("Name");
DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
comboboxColumn.DataPropertyName = "Name";
comboboxColumn.HeaderText = "Name";
comboboxColumn.DataSource = dataBS;
comboboxColumn.DisplayMember = "Name";
comboboxColumn.ValueMember = "Name";
dgv.Columns.Insert(0, comboboxColumn);
this.Controls.Add(dgv);
}
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError) { }
[STAThreadAttribute()]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new myForm());
}
}
}