ModelLinkControl modelLinkControl = new ModelLinkControl();
modelLinkControl.bindingSourceCModels.DataSource = cModels;
modelLinkControl.bindingSourceAModels.DataSource = aModels;
modelLinkControl.bindingSourceModelLinks.DataSource = modelLinks;
modelLinks is a List<MyClass>
containing 3 properties; ID, aID and cID. aID and cID is used for DataPropertyName in corresponding DataGridColum
.
aMode开发者_运维技巧ls and cModels are List<AnotherClass>
containing 2 properties; ID and Name, that I use for ValueMember = "ID"
and DisplayMember = "Name"
on corresponding ComboBox
.
On the last line of the snippet above i get;
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:
I have checked the spelling of all fields, classes and properties. I removed all Columns in the designer and re-added them again, carefully checking all spelling.
The ID's from modelLinks does exist in aModels and cModels. Also tried changing from List<>
to BindingList<>
on all 3 lists. And i just keep getting the same error.
If I remove ValueMember = "ID"
I do get another error (since AnotherClass
does not match a Guid
).
I don't know what to attempt next...
Designer generated:
//
// 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";
I just did this:
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;
}
}
In a new separate project, it works like a charm. At least that tells me I'm doing something wrong, and that it could be related to something else in the project that i haven't provided above. Nothing to do with the way I'm populating the DataGrid.
I will end this question and continue the hunt.
Update (Solution):
[BrowsableAttribute(false)]
is evil! :P
Its kinda obvious now when looking back at it. I wanted to hide the ID when displaying the class in a PropertyGrid with no thought that it would affect other controls as well. Really need to study Attributes further...
精彩评论