为什么DataGridView不显示数据绑定值?

时间:2014-10-21 15:50:01

标签: c# winforms datagridview bindingsource

我有一个中等复杂的情况,在那里我与一群BindingSource设置了亲子关系,所以我得到了:

DisposalBindingSource.DataSource = typeof(MyLibrary.Disposal);

DisposalCertificateBindingSource.DataMember = "DisposalCertificates";
DisposalCertificateBindingSource.DataSource = this.DisposalBindingSource;

AssetDisposalBindingSource.DataMember = "AssetDisposals";
AssetDisposalBindingSource.DataSource = this.DisposalBindingSource;

这是MyLibrary.Disposal看起来有点像的地方:

public class Disposal
{
    private EntitySet<AssetDisposal> _certificates;
    public ICollection<AssetDisposal> AssetDisposals
    {
        get
        {
            if (_certificates == null)
            {
                _certificates = new EntitySet<AssetDisposal>();
                _certificates.SetSource(AssetDisposal.Search(this)); // return value happens to be BindingList<AssetDisposal>; Use SetSource so that ListChangedEvents fire correctly.
            }

            return _certificates;
        }
    }

    private EntitySet<DisposalCertificate> _certificates;
    public ICollection<DisposalCertificate> DisposalCertificates
    {
        get
        {
            if (_certificates == null)
            {
                _certificates = new EntitySet<DisposalCertificate>();
                _certificates.SetSource(DisposalCertificate.Search(this)); // return value happens to be BindingList<DisposalCertificate>; Use SetSource so that ListChangedEvents fire correctly.
            }

            return _certificates;
        }
    }
}

现在,在BindingSource的{​​{1}}的{​​{1}}个DataGridView上使用了TabControl这两个子DataGridView,但我不明白为什么AssetDisposalBindingSource使用DisposalCertificateBindingSource的方法有效,但是使用DataBoundItem的方法(加载时不在活动标签页上)不起作用?

如果不工作,两个网格都会显示正确的行数,Count就是我所期望的,但网格中的列不会显示任何内容。可能导致此操作不显示数据绑定项的值的原因是什么?

不确定它是否相关,但设计师确实坚持为每个IsReadOnly的{​​{1}}和EntitySet<T>属性添加列。

1 个答案:

答案 0 :(得分:0)

就我而言,事实证明这是非常愚蠢的事情 - 我试图绑定的属性是private。将该属性设置为public可以解决问题。