【问题标题】:binding inherited objects to c# datagrid将继承的对象绑定到 c# datagrid
【发布时间】:2010-10-28 12:06:00
【问题描述】:

考虑以下类:

 class A : Idata
 {
      private int _id;
      //other private fields

      public int Id
      {
            get { return _id; }
            set { _id = value; }
      }
      //other property
 } 
 class B : A
 {
      private int _field;
      //other private fields

      public int Field
      {
            get { return _field; }
            set { _field = value; }
      }
      //other property
  }

  class BCollection : Collection
  {
     ////
  }

我正在尝试将 B 的集合(由 A 对象组成)绑定到数据网格,但出现以下错误: “对象'A'上的属性访问器'Id'抛出了闲置的执行:'对象与目标类型不匹配'” 事件虽然来自 A 的所有数据都到达 B

我该怎么办?

谢谢!

【问题讨论】:

  • “B 的集合(由 A 对象组成)”是什么意思?为了使这种数据绑定工作,您的集合需要是同质的。
  • “B 的集合(由 A 对象组成)”? -> 是的。但显然它有效!

标签: c# inheritance datagrid


【解决方案1】:

我明白了。这是我的错误,这就是我修复它的方法 Collection 继承自 CollectionBase,我使用了以下构造函数

 public IData this[int index]
        {
            get { return (IData)List[index]; }
            set { List[index] = value; }
        }

我所做的只是将它添加到 B 类中

public B this[int index]
        {
            get { return (B)List[index]; }
            set { List[index] = value; }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2012-03-15
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多