0
我有一個GridView,我有一列DataGridViewTextBoxColumn。 但是,當我給你我的數據源到網格我得到這個錯誤:DataGridViewComboBox和Gridview
的DataGridView默認錯誤對話框
發生在DataGridView以下異常:
System.FormatException: DataGridViewComboBoxCell value is not valid.
要替換此默認對話框,請處理DataError事件。
OK
我不明白這是爲什麼..
這是我的第一類:
public class FloraItem
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public DateTime ExpirationDate { get; set; }
public Category CategoryItem { get; set; }
}
這個類是網格的數據源
Category類似於:
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool AddBreaksToPrint { get; set; }
}
下拉(組合框)在網格中有分類數據源的數據源..
這是InitializeComponent函數
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.grdFloraItems = new System.Windows.Forms.DataGridView();
this.floraItemBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.categoryBindingSource2 = new System.Windows.Forms.BindingSource(this.components);
this.lstCategories = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.nameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.priceDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.expirationDateDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.CategoryItem = new System.Windows.Forms.DataGridViewComboBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.grdFloraItems)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.floraItemBindingSource)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.categoryBindingSource2)).BeginInit();
this.SuspendLayout();
//
// grdFloraItems
//
this.grdFloraItems.AllowUserToDeleteRows = false;
this.grdFloraItems.AutoGenerateColumns = false;
this.grdFloraItems.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.grdFloraItems.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.nameDataGridViewTextBoxColumn,
this.priceDataGridViewTextBoxColumn,
this.expirationDateDataGridViewTextBoxColumn,
this.CategoryItem});
this.grdFloraItems.DataSource = this.floraItemBindingSource;
this.grdFloraItems.Location = new System.Drawing.Point(12, 55);
this.grdFloraItems.Name = "grdFloraItems";
this.grdFloraItems.ReadOnly = true;
this.grdFloraItems.Size = new System.Drawing.Size(652, 206);
this.grdFloraItems.TabIndex = 1;
//
// floraItemBindingSource
//
this.floraItemBindingSource.DataSource = typeof(Data.FloraItem);
//
// categoryBindingSource2
//
this.categoryBindingSource2.DataSource = typeof(Data.Category);
//
// nameDataGridViewTextBoxColumn
//
this.nameDataGridViewTextBoxColumn.DataPropertyName = "Name";
this.nameDataGridViewTextBoxColumn.HeaderText = "Artikel";
this.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn";
this.nameDataGridViewTextBoxColumn.ReadOnly = true;
this.nameDataGridViewTextBoxColumn.Width = 250;
//
// priceDataGridViewTextBoxColumn
//
this.priceDataGridViewTextBoxColumn.DataPropertyName = "Price";
this.priceDataGridViewTextBoxColumn.HeaderText = "Prijs";
this.priceDataGridViewTextBoxColumn.Name = "priceDataGridViewTextBoxColumn";
this.priceDataGridViewTextBoxColumn.ReadOnly = true;
//
// expirationDateDataGridViewTextBoxColumn
//
this.expirationDateDataGridViewTextBoxColumn.DataPropertyName = "ExpirationDate";
this.expirationDateDataGridViewTextBoxColumn.HeaderText = "Verloop datum";
this.expirationDateDataGridViewTextBoxColumn.Name = "expirationDateDataGridViewTextBoxColumn";
this.expirationDateDataGridViewTextBoxColumn.ReadOnly = true;
this.expirationDateDataGridViewTextBoxColumn.Width = 150;
//
// CategoryItem
//
this.CategoryItem.DataPropertyName = "CategoryItem";
this.CategoryItem.DataSource = this.categoryBindingSource2;
this.CategoryItem.DisplayMember = "Name";
this.CategoryItem.HeaderText = "Categorie";
this.CategoryItem.Name = "CategoryItem";
this.CategoryItem.ReadOnly = true;
this.CategoryItem.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.CategoryItem.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.CategoryItem.ValueMember = "Id";
//
// EditFloraItem
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(696, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.lstCategories);
this.Controls.Add(this.grdFloraItems);
this.Name = "EditFloraItem";
this.Text = "EditFloraItem";
this.Controls.SetChildIndex(this.grdFloraItems, 0);
this.Controls.SetChildIndex(this.lstCategories, 0);
this.Controls.SetChildIndex(this.label1, 0);
((System.ComponentModel.ISupportInitialize)(this.grdFloraItems)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.floraItemBindingSource)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.categoryBindingSource2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.categoryBindingSource1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.categoryBindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
這是我很卡..
你的意思是在FloraItem類? (所以應該有一個List CategoryItem {get; set;})。 –
user1035498
2013-05-10 13:13:48
是的,但我會稱之爲CategoryItems。一個ComboBox需要綁定到一個集合。 – Paparazzi 2013-05-10 13:23:06