从父母名单中绑定儿童 [英] Binding Children From a List of Parents

查看:67
本文介绍了从父母名单中绑定儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将对象绑定到数据网格或组合框架时,我遇到过这个问题几次,我没有找到一个我非常满意的解决方案,所以我想我会把问题抛到那里。 />


想象一下,我有一个父母列表,每个父对象都包含一个子列表。假设我想列出数据网格中的所有父项,然后列出不同数据网格中的所有子项。我有一个父母列表,所以数据绑定很容易。然而,对于孩子们,我没有一个巨大的所有列表,他们是一堆列表,存储在他们的每个父母。有没有办法将数据绑定到这样一个我不知道的结构?



我知道一个解决方案就是拥有单独的父母和孩子的完整列表并创建每个Child对象中的ParentID,如果我需要选择单个Parent的Children,则允许我在Linq语句中加入两个Lists。但从逻辑上讲,将每个Parent的子列表放在Parent对象中是非常有意义的。除了绑定之外的一切,这很有用。我希望有办法可以做到这一点。



编辑:



澄清,我说的是以下类型的结构



I've come across this problem several times when trying to bind objects to data grids or comboboxes and I haven't found a solution I'm very happy with so I figured I'd throw the question out there.

Imagine I have a List of Parents, each parent object contains a List of Children. Lets say I want to list all the Parents in a Data Grid and then list all the Children in a different Data Grid. I have a List of Parents so databinding that is easy. However for the Children I don't have one giant list of all of them, they a bunch of Lists that are stored in each of their parents. Is there a way to databind to a structure like this that I am not aware of?

I know one solution is to have separate complete Lists of Parents and Children and create a ParentID in each Child object to allow me to join two Lists in a Linq statement if I need to select the Children of a single Parent. But logically it just makes so much sense to put a List of each Parent's Children in the Parent object. For everything other than binding, this works great. I'm hoping there is a way this can be done.



To clarify, I am talking about the below type of structure

public class Parents
{
    public string Spouse1Name { get; set; }
    public string Spouse2Name { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public string Name { get; set; }
}





然后我有列表< Parents>我的ModelView中的父母可用于数据绑定到所有父母。除了创建一个完整的其他List< child>包含孩子的对象,有没有办法将每个父母的 所有 的孩子数据绑定到 所有 (不只是单亲)?



I then have List<Parents> Parents in my ModelView that can be used to databind to all the Parents. Aside from creating a whole other List<child> object to contain the children, is there any way to databind to all the children of all each of the parents (not just a single parent)?

推荐答案

引用:

但从逻辑上讲将每个Parent的子列表放在Parent对象中是非常有意义的。除了绑定之外的一切,这很好用

But logically it just makes so much sense to put a List of each Parent's Children in the Parent object. For everything other than binding, this works great



你在这里是正确的。这种方法也快速而简单。几个指针供你开始。



1)你需要两个课程。 1名儿童和1名父母。您的父类应包含子类列表对象,以便您可以拥有一个父对象的所有数据。



ex:


You are correct here. And the approach is also quick and easy. Few Pointers for you to start.

1) You need two Classes. 1 Child and 1 Parent. Your Parent Class should contain a Child Class List object so that you can have all data of one parent object.

ex:

class ChildData
    {
        public int CClassID { get; set; }
        public String CClassValue { get; set; }
    }

    class ParentData
    {
        public int ParentClassID { get; set; }
        public String ParentClassName { get; set; }
        public List<childdata> ChildList { get; set; }
    } 





现在你的 ParentData 拥有所有信息。将 ParentClassID ParentClassName 绑定到First DataGridView或ComboBox,并根据索引获取 ChildData ParentData 中的元素,并将其绑定到第二个DataGridView或ComboBox。



如果你有更具体的数据结构,方法是相同的,但你可能需要根据你的要求改变你的逻辑。



AVK



Now your ParentData has all the info. Bind ParentClassID and ParentClassName to First DataGridView or ComboBox and based on the Index, Get the ChildData Element from ParentData and Bind it to your Second DataGridView or ComboBox.

If you have more specific Data Structure, Approach is same but you may need to Change your logic a little based on your requirement.

AVK


这篇关于从父母名单中绑定儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆