简体   繁体   English

如何使用五元嵌套列表创建和绑定网格以显示C#ASP.net .aspx

[英]How to Create and Bind the Grid for Display With Quintuple Nested Lists C# ASP.net .aspx

I'm working on a project where it is necessary for me to create and bind the grid to display a response from a server which is a list of classes and some fields, the lists of classes also contain some variables as well as another list of a different class which contains some fields and another list of a different class...and goes on for five levels. 我正在开发一个项目,我需要创建并绑定网格以显示来自服务器的响应,该服务器是类列表和一些字段,类列表还包含一些变量以及另一个列表一个不同的类,包含一些字段和另一个不同类的列表......并继续五个级别。

I must display the top level class and all of the lists of classes as well as each of the nested lists within the list and by itself. 我必须显示顶级类和所有类列表以及列表中的每个嵌套列表以及它自己。 Allow me to use pseudocode to try to better explain with a triple tier. 允许我使用伪代码来尝试更好地解释三层。 I am dealing with a quintuple tier. 我正在处理一个五元组。

classA
{
List<classB> classBList;
List<classC> classCList;
int whatever;
string something;
}

ClassB
{
List<classC> classCList;
int somethingElse;
string otherThing;
}

classC
{
int somethingA;
string somethingB;
}

List<ClassA> list1;

I am trying to create and bind and display the grid for list1. 我正在尝试创建并绑定并显示list1的网格。 I've mainly been a straight back end coder so the .aspx page is what is really throwing me for a loop. 我主要是一个直接的后端编码器,所以.aspx页面真的让我失去了一个循环。 I've figured out how bind and display with fields and fields within classes and a single list, but these lists are really challenging for me and I haven't made any progress in a couple of days. 我已经弄清楚如何绑定和显示类和单个列表中的字段和字段,但这些列表对我来说真的很有挑战性,而且我在几天内没有取得任何进展。

Any help is much appreciated! 任何帮助深表感谢!

Maybe something like where you flatten all the values down and use a dictionary to track their origin (if it's needed). 也许就像你将所有值压平并使用字典来追踪它们的起源(如果需要的话)。 You would then have a flattened list of all values, and could easily create or recreate an original list of values (those belonging to ClassA.ClassBList, for example) using Linq or the Dictionary keys themselves: 然后,您将拥有所有值的展平列表,并且可以使用Linq或Dictionary键本身轻松创建或重新创建原始值列表(例如,属于ClassA.ClassBList的值):

public class flattenedList
{
    public string whatever;
    public int whateverInt;
}
public class nested
{
    private Dictionary<string, List<flattenedList>> listData = new Dictionary<string, List<flattenedList>>();
    private List<ClassA> list1 = new List<ClassA>();
    ClassA classA = new ClassA();
    ClassB classB = new ClassB();
    ClassC classC = new ClassC();
    public void processCalsses()
    {
        string key = "";
        foreach (ClassA a in list1)
        {
            key = "ClassA.ClassBList";
            foreach (ClassB b in classA.classBList)
            {
                addToDictionary(key, new flattenedList() { whatever = b.otherThing, whateverInt = b.somethingElse });
            }
            key = "ClassA.ClassCList";
            foreach (ClassC c in classA.classCList)
            {
                addToDictionary(key, new flattenedList() { whatever = c.somethingB, whateverInt = c.somethingA });
            }
            addToDictionary("ClassA", new flattenedList() { whatever = a.something, whateverInt = a.whatever });
        }
        key = "ClassB.ClassCList";
        foreach (ClassC c in classB.classCList)
        {
            addToDictionary(key, new flattenedList() { whatever = c.somethingB, whateverInt = c.somethingA });
        }
        addToDictionary("ClassB", new flattenedList() { whatever = classB.otherThing, whateverInt = classB.somethingElse });
        addToDictionary("ClassC", new flattenedList() { whatever = classC.somethingB, whateverInt = classC.somethingA });
        foreach (KeyValuePair<string, List<flattenedList>> kvp in listData)
        {
            for (int i = 0; i < kvp.Value.Count; i++)
            {
                Console.WriteLine(key + "[" + i.ToString() + "] whatever = " + kvp.Value[i].whatever);
                Console.WriteLine(key + "[" + i.ToString() + "] whateverInt = " + kvp.Value[i].whateverInt.ToString() + "\n");
            }
        }
    }
    private void addToDictionary(string key, flattenedList f)
    {
        if (!listData.ContainsKey(key))
        {
            listData.Add(key, new List<flattenedList>());
        }
        listData[key].Add(f);
    }
    public class ClassA
    {
        public List<ClassB> classBList = new List<ClassB>();
        public List<ClassC> classCList;
        public int whatever;
        public string something;
    }

    public class ClassB
    {
        public List<ClassC> classCList;
        public int somethingElse;
        public string otherThing;
    }

    public class ClassC
    {
        public int somethingA;
        public string somethingB;
    }


}

Note that I had to instantiate the classes in order to get the intellisense to let me type. 请注意,我必须实例化类才能获得intellisense让我输入。 I assume you'd create instance versions visible to this method either within class scope or within a public static class. 我假设您要在类范围内或公共静态类中创建此方法可见的实例版本。

PS - if you don't need to maintain the origin of each datum (which class and list it came from), then there's an easier way to do this. PS - 如果你不需要维护每个数据的来源(它来自哪个类和列表),那么有一种更简单的方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM