我正在嘗試使用DataGridView與業務對象。這裏有一個簡單的場景: - 調用對象的「配方」,它有幾個成分的的BindingList在配方 對象 - 我也有一個「數據庫」,這是所有可用的成分的的BindingList對象WinForms數據綁定DataGridView與ComboBoxColum
我想在配方中顯示所有配料的DataGridView,並允許用戶在名稱字段中的組合框中選擇新的配料。
當我嘗試按照我認爲應該的方式進行設置時,食譜中配料的名稱不顯示,但我可以從IngredientDB中進行選擇,並選擇一種新配料以更新食譜中的列表。
如何讓名稱正確顯示?
這是我到目前爲止有:
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());
}
}
}
我的作品,我拿飯,全麥麪粉和下拉黃油作爲價值。 – 2009-07-20 21:09:18