Winforms - 创建动态表的问题

时间:2018-01-09 18:16:36

标签: c# winforms

我有一个Windows窗体应用程序,其中我有多个表的视图。通过按对象的属性设置列的名称,然后将它们添加到数据集,可以动态构建表。还为每个表添加了一个空行。我遇到的问题是,在用于创建这些表的9个对象中,有2个表未设置任何列或空行。

我已经查看了调试,并将获取列的对象属性与没有列的对象进行了比较,并且无法解释问题所在。以前有没有人遇到过这个问题?如果是这样,问题是什么?

这是我的代码,请告诉我是否还需要其他信息:

protected override DataSet GetDefaultFilterTypes()
    {
        return DataSetUtility.ToDataSet(new List<Type> { typeof(Counterparty), typeof(Address), typeof(Contact),
            typeof(Customer), typeof(Broker), typeof(ExchangeBroker), typeof(ExchangeBrokerAccount), typeof(Supplier), typeof(SupplierTerminalPriceMapping) });
    }


public static DataSet ToDataSet(IList<Type> list)
    {
        DataSet ds = new DataSet();

        foreach (Type item in list)
        {
            DataTable ta = new DataTable(item.ToString());
            ds.Tables.Add(ta);

            //add a column to table for each public property on T
            foreach (var propInfo in item.GetProperties())
            {
                Type colType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;
                ta.Columns.Add(propInfo.Name, colType);
            }

            DataRow row = ta.NewRow();

            foreach (var propInfo in item.GetProperties())
            {
                row[propInfo.Name] = DBNull.Value;
            }

            ta.Rows.Add(row);
        }

        return ds;
    }

这里是ExchangeBroker和Supplier的类定义 - 没有获取列的对象:

public partial class Supplier : MomentumEntityBase
{
    #region *** Properties

    [DataMember(Order = 1, IsRequired = true)]
    [Key,Required]
    [GreaterThanZero]
    public int SupplierId
    {
        get { return _supplierId; }
        set
        {
            if (Equals(value, _supplierId)) return;
            _supplierId = value;
            NotifyPropertyChanged("SupplierId");
        }
    }
    private int _supplierId;


    [DataMember(Order = 2, IsRequired = true)]
    [Required(AllowEmptyStrings = false),StringLength(64)]
    public string CreationName
    {
        get { return _creationName; }
        set
        {
            if (Equals(value, _creationName)) return;
            _creationName = value;
            NotifyPropertyChanged("CreationName");
        }
    }
    private string _creationName;


    [DataMember(Order = 3, IsRequired = true)]
    [Required]
    public DateTime CreationDate
    {
        get { return _creationDate; }
        set
        {
            if (Equals(value, _creationDate)) return;
            _creationDate = value;
            NotifyPropertyChanged("CreationDate");
        }
    }
    private DateTime _creationDate;


    [DataMember(Order = 4, IsRequired = false)]
    [StringLength(64)]
    public string RevisionName
    {
        get { return _revisionName; }
        set
        {
            if (Equals(value, _revisionName)) return;
            _revisionName = value;
            NotifyPropertyChanged("RevisionName");
        }
    }
    private string _revisionName;


    [DataMember(Order = 5, IsRequired = false)]

    public DateTime? RevisionDate
    {
        get { return _revisionDate; }
        set
        {
            if (Equals(value, _revisionDate)) return;
            _revisionDate = value;
            NotifyPropertyChanged("RevisionDate");
        }
    }
    private DateTime? _revisionDate;


    #endregion

    #region *** Reverse Navigation

    #endregion

    #region *** Foreign Keys

    // Foreign keys
    public Counterparty Counterparty
    {
        get { return _counterparty; }
        set
        {
            if (Equals(value, _counterparty)) return;
            _counterparty = value;
            CounterpartyChangeTracker = _counterparty == null ? null : new ChangeTrackingCollection<Counterparty> { _counterparty };
             NotifyPropertyChanged("Counterparty");
        }
    }
    private Counterparty _counterparty;
    private ChangeTrackingCollection<Counterparty> CounterpartyChangeTracker { get; set; }

    #endregion

    #region *** CTOR / Initialize

    public Supplier()
    {
        InitializePartial();
    }

    partial void InitializePartial();

    #endregion

    #region *** Error

    public override string Error
    {
        get
        {
            var error = new StringBuilder();

            if (TrackingState != TrackableEntities.TrackingState.Deleted)
            {
                if (!string.IsNullOrEmpty(base.Error))
                {
                    error.AppendLine(base.Error);
                }

            }

            return error.ToString();
        }
    }

    #endregion
}


 [DataContract] 
public enum ExchangeBrokerColumns
{
    [EnumMember]
    ExchangeBrokerId = 0,
    [EnumMember]
    CreationName = 1,
    [EnumMember]
    CreationDate = 2,
    [EnumMember]
    RevisionName = 3,
    [EnumMember]
    RevisionDate = 4
}


// ExchangeBroker
//[DataContract]
[GeneratedCodeAttribute("EF.Reverse.POCO.Generator", "2.17.1.0")]
public partial class ExchangeBroker : MomentumEntityBase
{
    #region *** Properties

    [DataMember(Order = 1, IsRequired = true)]
    [Key,Required]
    [GreaterThanZero]
    public int ExchangeBrokerId
    {
        get { return _exchangeBrokerId; }
        set
        {
            if (Equals(value, _exchangeBrokerId)) return;
            _exchangeBrokerId = value;
            NotifyPropertyChanged("ExchangeBrokerId");
        }
    }
    private int _exchangeBrokerId;


    [DataMember(Order = 2, IsRequired = true)]
    [Required(AllowEmptyStrings = false),StringLength(64)]
    public string CreationName
    {
        get { return _creationName; }
        set
        {
            if (Equals(value, _creationName)) return;
            _creationName = value;
            NotifyPropertyChanged("CreationName");
        }
    }
    private string _creationName;


    [DataMember(Order = 3, IsRequired = true)]
    [Required]
    public DateTime CreationDate
    {
        get { return _creationDate; }
        set
        {
            if (Equals(value, _creationDate)) return;
            _creationDate = value;
            NotifyPropertyChanged("CreationDate");
        }
    }
    private DateTime _creationDate;


    [DataMember(Order = 4, IsRequired = false)]
    [StringLength(64)]
    public string RevisionName
    {
        get { return _revisionName; }
        set
        {
            if (Equals(value, _revisionName)) return;
            _revisionName = value;
            NotifyPropertyChanged("RevisionName");
        }
    }
    private string _revisionName;


    [DataMember(Order = 5, IsRequired = false)]

    public DateTime? RevisionDate
    {
        get { return _revisionDate; }
        set
        {
            if (Equals(value, _revisionDate)) return;
            _revisionDate = value;
            NotifyPropertyChanged("RevisionDate");
        }
    }
    private DateTime? _revisionDate;


    #endregion

    #region *** Reverse Navigation

    // Reverse navigation
    public ChangeTrackingCollection<ExchangeBrokerAccount> ExchangeBrokerAccounts
    {
        get { return _exchangeBrokerAccounts; }
        set
        {
            if (Equals(value, _exchangeBrokerAccounts)) return;
            _exchangeBrokerAccounts = value;
             //NotifyPropertyChanged("ExchangeBrokerAccounts");
        }
    }
    private ChangeTrackingCollection<ExchangeBrokerAccount> _exchangeBrokerAccounts;

    #endregion

    #region *** Foreign Keys

    // Foreign keys
    public Counterparty Counterparty
    {
        get { return _counterparty; }
        set
        {
            if (Equals(value, _counterparty)) return;
            _counterparty = value;
            CounterpartyChangeTracker = _counterparty == null ? null : new ChangeTrackingCollection<Counterparty> { _counterparty };
             NotifyPropertyChanged("Counterparty");
        }
    }
    private Counterparty _counterparty;
    private ChangeTrackingCollection<Counterparty> CounterpartyChangeTracker { get; set; }

    #endregion

    #region *** CTOR / Initialize

    public ExchangeBroker()
    {
        ExchangeBrokerAccounts = new ChangeTrackingCollection<ExchangeBrokerAccount>();
        InitializePartial();
    }

    partial void InitializePartial();

    #endregion

    #region *** Error

    public override string Error
    {
        get
        {
            var error = new StringBuilder();

            if (TrackingState != TrackableEntities.TrackingState.Deleted)
            {
                if (!string.IsNullOrEmpty(base.Error))
                {
                    error.AppendLine(base.Error);
                }

                if (ExchangeBrokerAccounts.HasError())
                {
                    error.AppendLine("ExchangeBrokerAccounts contains validation error(s).");
                }

            }

            return error.ToString();
        }
    }

    #endregion
}

0 个答案:

没有答案