0

I've sourced my data for a treeview using the Proces datastructure below. expanded simply indicates whether or not the tree item has been expanded to show all of its children. This is my attempt to iterate through a map all showing items into a 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; }
}

I'm trying to map this out into a DataTable, but i keep getting a stack overflow.

        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);
                    }
                }
            }
        }
6
  • That a typo? You're iterating over "processes" when your method argument is called "proccess". Commented Mar 16, 2018 at 14:45
  • It's much easier to spot the error if you remove unnessesary code (the drNew=-lines) and tell if i.e. proc.expanded is true.I guess you made a mistake at the recursive call : generateShownTree(proc.subProcesses); -> generateShownTree(subProc.subProcesses); Commented Mar 16, 2018 at 14:46
  • You're not traversing a list, you're traversing a tree. Big difference. Commented Mar 16, 2018 at 14:48
  • Look for two processes who have each other the parent. Commented Mar 16, 2018 at 14:48
  • @Chriz Isn't it cleaner to do it this way rather than writing it all out as a string passed to rows.Add? Commented Mar 16, 2018 at 14:55

1 Answer 1

1

I assume you don't want to iterate the list of subprocesses as well as invoke the generateShownTree method recursively. I also changed the name of the argument passed to generateShownTree to match the object being iterated.

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.