数据绑定子类

发布于 2024-07-16 06:07:23 字数 688 浏览 2 评论 0原文

假设我有这 3 个类:

public class ClassParent  
{  
public string TestParent { get; set; }  
}

public class ClassChild1 : ClassParent   
{   
public string TestChild1 { get; set; }   
}

public class ClassChild2 : ClassParent
{  
public string TestChild2 { get; set; }  
}

假设我创建了很多类型为 ClassChild1 和 ClassChild2 的对象,并将它们存储在这个列表中:
列表< 父类> _测试;

我想将此列表绑定到 GridView
MyGridView.DataSource=_Test;

这是可行的,但它只显示网格中的一个字段(位于 ClassParent 类中的 TestParent 属性)。 我明白为什么:该列表由“ClassParent”对象组成,因此绑定仅使用该类的属性完成。 问题是 ClassChild1 和 ClassChild2 类型的对象继承自 ClassParent。

我的问题是:如果我有一个不同类型的对象列表,但它们都继承自同一类,我如何将所有属性绑定到网格?

Say I have these 3 classes:

public class ClassParent  
{  
public string TestParent { get; set; }  
}

public class ClassChild1 : ClassParent   
{   
public string TestChild1 { get; set; }   
}

public class ClassChild2 : ClassParent
{  
public string TestChild2 { get; set; }  
}

Say, I've created plenty of objects of type ClassChild1 and ClassChild2 that I've stored in this List:
List< ClassParent> _Test;

I want to bind this list to a GridView
MyGridView.DataSource=_Test;

This works but it only shows one field in the grid (the TestParent property which is in the ClassParent class). I understand why: the list is made of 'ClassParent' objects so the binding is only done with the properties of that class. The thing is the objects of type ClassChild1 and ClassChild2 inherit from ClassParent.

My question is: if I have a list of objects of different type but which all inherit from the same class, how I can bind all the properties to the grid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-07-23 06:07:23

这些属性必须存在于基类中。 否则,如何在未定义它们的子类中访问它们? (您希望网格为 ClassChild2.TestChild1 显示什么?)

解决此问题的一种方法是定义 TestChild1TestChild2< /code> 作为 ClassParent 对象的虚拟属性,然后在子对象中覆盖它们:

public class ClassParent  
{  
    public string TestParent { get; set; }  
    public virtual string TestChild1 { get {return null;}}
    public virtual string TestChild2 { get {return null;}}  
}

public class ClassChild1 : ClassParent   
{   
    public override string TestChild1 { get; set; }   
}

public class ClassChild2 : ClassParent
{  
    public override string TestChild2 { get; set; }  
}

The properties have to be present in the base class. Otherwise, how would you access them in a subclass where they're not defined? (What would you like the grid to show for ClassChild2.TestChild1?)

One way around this to fit your model that should work is to define TestChild1 and TestChild2 as virtual properties of your ClassParent object, then override them in the children:

public class ClassParent  
{  
    public string TestParent { get; set; }  
    public virtual string TestChild1 { get {return null;}}
    public virtual string TestChild2 { get {return null;}}  
}

public class ClassChild1 : ClassParent   
{   
    public override string TestChild1 { get; set; }   
}

public class ClassChild2 : ClassParent
{  
    public override string TestChild2 { get; set; }  
}

您可以手动创建列,而不是从类型推断它们。 这样,您就可以绑定到您想要的任何属性。

You can create the columns manually rather than having them inferred from the type. That way, you can bind to whatever properties you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文