0

我有一个 GridView,我有一列 DataGridViewTextBoxColumn。但是当我将我的 DataSource 分配给 Grid 时,我得到了这个错误:

DataGridView 默认错误对话框

DataGridView 出现以下异常:

System.FormatException: DataGridViewComboBoxCell value is not valid.

要替换此默认对话框,请处理 DataError 事件。

好的

我不明白这是为什么..

这是我的第一堂课:

    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; }
    }

这个类是网格的DataSource

类别类看起来像:

   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();

      }

这就是我卡住的地方..

4

1 回答 1

0

类别需要是一个集合(如列表)而不是单个项目。
你可能会称之为类别。

于 2013-05-10T13:10:42.047 回答