ModelLinkControl modelLinkControl = new ModelLinkControl();
modelLinkControl.bindingSourceCModels.DataSource = cModels;
modelLinkControl.bindingSourceAModels.DataSource = aModels;
modelLinkControl.bindingSourceModelLinks.DataSource = modelLinks;
modelLinks 是一个List<MyClass>
包含 3 个属性的;ID、aID 和 cID。aID 和 cID 用于对应的 DataPropertyName DataGridColum
。aModels 和 cModelsList<AnotherClass>
包含 2 个属性;ID 和名称,我用于ValueMember = "ID"
和DisplayMember = "Name"
对应ComboBox
的 .
在上面片段的最后一行我得到;
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:
我检查了所有字段、类和属性的拼写。我删除了设计器中的所有列并重新添加它们,仔细检查所有拼写。modelLinks 的 ID 确实存在于 aModels 和 cModels 中。还尝试在所有 3 个列表上从 更改List<>
为。BindingList<>
我只是不断收到同样的错误。
如果我删除ValueMember = "ID"
我会得到另一个错误(因为AnotherClass
不匹配 a 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";