簡體   English   中英

如何使用五元嵌套列表創建和綁定網格以顯示C#ASP.net .aspx

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

我正在開發一個項目,我需要創建並綁定網格以顯示來自服務器的響應,該服務器是類列表和一些字段,類列表還包含一些變量以及另一個列表一個不同的類,包含一些字段和另一個不同類的列表......並繼續五個級別。

我必須顯示頂級類和所有類列表以及列表中的每個嵌套列表以及它自己。 允許我使用偽代碼來嘗試更好地解釋三層。 我正在處理一個五元組。

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;

我正在嘗試創建並綁定並顯示list1的網格。 我主要是一個直接的后端編碼器,所以.aspx頁面真的讓我失去了一個循環。 我已經弄清楚如何綁定和顯示類和單個列表中的字段和字段,但這些列表對我來說真的很有挑戰性,而且我在幾天內沒有取得任何進展。

任何幫助深表感謝!

也許就像你將所有值壓平並使用字典來追蹤它們的起源(如果需要的話)。 然后,您將擁有所有值的展平列表,並且可以使用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;
    }


}

請注意,我必須實例化類才能獲得intellisense讓我輸入。 我假設您要在類范圍內或公共靜態類中創建此方法可見的實例版本。

PS - 如果你不需要維護每個數據的來源(它來自哪個類和列表),那么有一種更簡單的方法可以做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM