系统.当尝试使用DataGridViewComboBox和ValueMember时,出现ArgumentExceptio

本文关键字:ValueMember 出现 ArgumentExceptio DataGridViewComboBox 系统 | 更新日期: 2023-09-27 18:04:36

ModelLinkControl modelLinkControl = new ModelLinkControl();
modelLinkControl.bindingSourceCModels.DataSource = cModels;
modelLinkControl.bindingSourceAModels.DataSource = aModels;
modelLinkControl.bindingSourceModelLinks.DataSource = modelLinks;

modelllinks是包含3个属性的List<MyClass>;ID, aID和cID。对应DataGridColum中的DataPropertyName使用"aID"answers"cID"。models和cModels是包含2个属性的List<AnotherClass>;ID和Name,我在相应的ComboBox上为ValueMember = "ID"DisplayMember = "Name"使用。

在上面代码片段的最后一行,我得到;

System.ArgumentException occurred
  Message=Field called ID does not exist.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridViewComboBoxCell.InitializeValueMemberPropertyDescriptor(String valueMember)
       at System.Windows.Forms.DataGridViewComboBoxCell.OnDataGridViewChanged()
       at System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow dataGridViewRow)
       at System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView)
       at System.Windows.Forms.DataGridView.RefreshColumnsAndRows()
       at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
       at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
       at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
       at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
       at System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)
       at System.Windows.Forms.BindingSource.ResetBindings(Boolean metadataChanged)
       at System.Windows.Forms.BindingSource.SetList(IList list, Boolean metaDataChanged, Boolean applySortAndFilter)
       at System.Windows.Forms.BindingSource.ResetList()
       at System.Windows.Forms.BindingSource.set_DataSource(Object value)
  InnerException: 

我检查了所有字段、类和属性的拼写。我删除了设计器中的所有列,然后重新添加,仔细检查了所有的拼写。modellink中的ID确实存在于models和cModels中。也尝试从List<>更改到BindingList<>在所有3个列表。我总是得到相同的错误

如果我删除ValueMember = "ID",我确实得到另一个错误(因为AnotherClass不匹配Guid)。

我不知道下一步该做什么…

设计师生成:

// 
// dataGridViewModels
// 
this.dataGridViewModels.AllowUserToAddRows = false;
this.dataGridViewModels.AllowUserToDeleteRows = false;
this.dataGridViewModels.AutoGenerateColumns = false;
this.dataGridViewModels.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dataGridViewModels.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
this.dataGridViewModels.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewModels.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.ColumnC,
this.ColumnA});
this.dataGridViewModels.DataSource = this.bindingSourceModelLinks;
this.dataGridViewModels.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridViewModels.Location = new System.Drawing.Point(0, 0);
this.dataGridViewModels.Name = "dataGridViewModels";
this.dataGridViewModels.RowHeadersVisible = false;
this.dataGridViewModels.Size = new System.Drawing.Size(794, 445);
this.dataGridViewModels.TabIndex = 0;
// 
// ColumnC
// 
this.ColumnC.DataPropertyName = "cID";
this.ColumnC.DataSource = this.bindingSourceCModels;
this.ColumnC.HeaderText = "cModel";
this.ColumnC.Name = "ColumnC";
this.ColumnC.DisplayMember = "Name";
this.ColumnC.ValueMember = "ID";
// 
// ColumnA
// 
this.ColumnA.DataPropertyName = "aID";
this.ColumnA.DataSource = this.bindingSourceAModels;
this.ColumnA.HeaderText = "aModel";
this.ColumnA.Name = "ColumnA";
this.ColumnA.DisplayMember = "Name";
this.ColumnA.ValueMember = "ID";

系统.当尝试使用DataGridViewComboBox和ValueMember时,出现ArgumentExceptio

我刚刚这样做了:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestDataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.PopulateDataGrid();
        }
        private void PopulateDataGrid()
        {
            List<BindID> bindList = new List<BindID>();
            List<ClassA> aList = new List<ClassA>();
            List<ClassB> bList = new List<ClassB>();
            for (int i = 0; i < 10; i++)
            {
               ClassA newClassA = new ClassA() { ID = Guid.NewGuid(), Name = i.ToString() };
               ClassB newClassB = new ClassB() { ID = Guid.NewGuid(), Name = i.ToString() };
               BindID newBindID = new BindID() { ID = Guid.NewGuid(), AID = newClassA.ID, BID = newClassB.ID };
               bList.Add(newClassB);
               aList.Add(newClassA);
               bindList.Add(newBindID);
            }
            bindingSourceA.DataSource = aList;
            ColumnA.ValueMember = "ID";
            ColumnA.DisplayMember = "Name";
            bindingSourceB.DataSource = bList;
            ColumnB.ValueMember = "ID";
            ColumnB.DisplayMember = "Name";
            bindingSourceBindID.DataSource = bindList;
        }
    }
    public class BindID
    {
        public Guid ID { get; set; }
        public Guid AID { get; set; }
        public Guid BID { get; set; }
    }
    public class ClassA
    {
        public Guid ID { get; set; }
        public string Name { get; set; }
    }
    public class ClassB
    {
        public Guid ID { get; set; }
        public string Name { get; set; }
    }
}

Form1.Designer.cs

namespace TestDataGridView
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.bindingSourceBindID = new System.Windows.Forms.BindingSource(this.components);
            this.bindingSourceA = new System.Windows.Forms.BindingSource(this.components);
            this.bindingSourceB = new System.Windows.Forms.BindingSource(this.components);
            this.ColumnA = new System.Windows.Forms.DataGridViewComboBoxColumn();
            this.ColumnB = new System.Windows.Forms.DataGridViewComboBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceBindID)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceA)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceB)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ColumnA,
            this.ColumnB});
            this.dataGridView1.DataSource = this.bindingSourceBindID;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(709, 407);
            this.dataGridView1.TabIndex = 0;
            // 
            // ColumnA
            // 
            this.ColumnA.DataPropertyName = "AID";
            this.ColumnA.DataSource = this.bindingSourceA;
            this.ColumnA.HeaderText = "ColumnA";
            this.ColumnA.Name = "ColumnA";
            // 
            // ColumnB
            // 
            this.ColumnB.DataPropertyName = "BID";
            this.ColumnB.DataSource = this.bindingSourceB;
            this.ColumnB.HeaderText = "ColumnB";
            this.ColumnB.Name = "ColumnB";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(709, 407);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceBindID)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceA)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSourceB)).EndInit();
            this.ResumeLayout(false);
        }
        #endregion
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.BindingSource bindingSourceBindID;
        private System.Windows.Forms.BindingSource bindingSourceA;
        private System.Windows.Forms.BindingSource bindingSourceB;
        private System.Windows.Forms.DataGridViewComboBoxColumn ColumnA;
        private System.Windows.Forms.DataGridViewComboBoxColumn ColumnB;
    }
}

在一个新的独立项目中,它像一个魅力。至少这告诉我我做错了什么,它可能与我上面没有提供的项目中的其他东西有关。与我填充DataGrid的方式无关。

我将结束这个问题,继续寻找。

更新(解决方案):[BrowsableAttribute(false)]是邪恶的!: P现在回头看,这一点很明显。我想在PropertyGrid中显示类时隐藏ID,而不考虑它也会影响其他控件。真的需要进一步研究属性