1
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";
4

1 回答 1

2

我只是这样做了:

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,而不考虑它也会影响其他控件。真的需要进一步研究属性...

于 2011-08-04T09:04:24.080 回答