列表遍历上的堆栈溢出

时间:2018-03-16 14:37:33

标签: c#

我使用下面的Proces数据结构为树视图提供了数据。 expand仅指示树项是否已展开以显示其所有子项。这是我尝试迭代遍历所有显示项目到DataTable的地图。

public class Proces
{
    public string PN { get; set; }
    public string Description { get; set; }
    public int Qty { get; set; }
    public string PartType { get; set; }
    public decimal PricePer { get; set; }
    public string Mfr { get; set; }
    public int Stock { get; set; }
    public int OnOrder { get; set; }
    public string parent { get; set; }
    public bool expanded { get; set; } = false;

    public List<Proces> subProcesses { get; set; }
}

我试图将其映射到DataTable中,但我不断收到堆栈溢出。

        void generateShownTree(List<Proces> proccess)
        {
            foreach (Proces proc in processes)
            {
                DataRow drNew = export.NewRow();
                drNew["Parent"] = proc.parent;
                drNew["PN"] = proc.PN;
                drNew["Description"] = proc.Description;
                drNew["Qty"] = proc.Qty;
                drNew["PartType"] = proc.PartType;
                drNew["PricePer"] = proc.PricePer;
                drNew["Mfr"] = proc.Mfr;
                drNew["Stock"] = proc.Stock;
                drNew["OnOrder"] = proc.OnOrder;
                export.Rows.Add(drNew);

                if (proc.expanded == true)
                {
                    foreach (Proces subProc in proc.subProcesses)
                    {
                        subProc.parent = proc.PN;
                        drNew = export.NewRow();
                        drNew["Parent"] = subProc.parent;
                        drNew["PN"] = subProc.PN;
                        drNew["Description"] = subProc.Description;
                        drNew["Qty"] = subProc.Qty;
                        drNew["PartType"] = subProc.PartType;
                        drNew["PricePer"] = subProc.PricePer;
                        drNew["Mfr"] = subProc.Mfr;
                        drNew["Stock"] = subProc.Stock;
                        drNew["OnOrder"] = subProc.OnOrder;
                        export.Rows.Add(drNew);

                        generateShownTree(proc.subProcesses);
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

我假设您不想迭代子进程列表以及递归调用generateShownTree方法。我还更改了传递给generateShownTree的参数的名称以匹配正在迭代的对象。

static void generateShownTree(List<Proces> processes)
{
    foreach (Proces proc in processes)
    {
        DataRow drNew = export.NewRow();
        drNew["Parent"] = proc.parent;
        drNew["PN"] = proc.PN;
        drNew["Description"] = proc.Description;
        drNew["Qty"] = proc.Qty;
        drNew["PartType"] = proc.PartType;
        drNew["PricePer"] = proc.PricePer;
        drNew["Mfr"] = proc.Mfr;
        drNew["Stock"] = proc.Stock;
        drNew["OnOrder"] = proc.OnOrder;
        export.Rows.Add(drNew);

        if (proc.expanded)
        {
            generateShownTree(proc.subProcesses);
        }
    }
}