Problems Deleting Child Nodes
ObjectListView - ListView on caffeine, guarana and steroids
Brought to you by:
grammarian
I have searched and searchedm but cannot find an answer to my problem. I have command button the opn the Click event should delete current select row. It worls for root level node, but not on a selected child.
Why ?
My delete function is at the bottom of the code below:
public class Node { public string Name { get; set; } public string OffSet { get; set; } public string Description { get; set; } public string Size { get; set; } public bool Base { get; set; } public List<Node> Children { get; set; } // constructor public Node(string name, string offset, string size, string description, bool blnbase=false) { this.Name = name; this.OffSet = offset; this.Size = size; this.Description = description; this.Children = new List<Node>(); this.Base = blnbase; } } public List<Node> RegsData; private void FillTree() { // set the delegate that the tree uses to know if a node is expandable this.otlvRegs.CanExpandGetter = x => (x as Node).Children.Count > 0; // set the delegate that the tree uses to know the children of a node this.otlvRegs.ChildrenGetter = x => (x as Node).Children; this.otlvRegs.SelectionChanged += delegate (object sender, EventArgs args) { SelectionChanged(sender, args); }; // create the tree columns and set the delegates to print the desired object proerty var nameCol = new BrightIdeasSoftware.OLVColumn("Name", "Name"); nameCol.Width = 160; nameCol.AspectGetter = x => (x as Node).Name; //nameCol.ImageIndex = 0; var offsetCol = new BrightIdeasSoftware.OLVColumn("OffSet", "OffSet"); offsetCol.AspectGetter = x => (x as Node).OffSet; var sizeCol = new BrightIdeasSoftware.OLVColumn("Size", "Size"); sizeCol.AspectGetter = x => (x as Node).Size; var descriptionCol = new BrightIdeasSoftware.OLVColumn("Description", "Description"); descriptionCol.FillsFreeSpace = true; descriptionCol.AspectGetter = x => (x as Node).Description; // add the columns to the tree this.otlvRegs.Columns.Add(nameCol); this.otlvRegs.Columns.Add(offsetCol); this.otlvRegs.Columns.Add(sizeCol); this.otlvRegs.Columns.Add(descriptionCol); nameCol.ImageGetter = delegate (object row) { if (((Node)row).Base) return 0; else return 1; }; // set the tree roots this.otlvRegs.Roots = RegsData; } private void btnDelete_Click(object sender, EventArgs e) { object c = otlvRegs.SelectedObject; object p = otlvRegs.GetParent(c); otlvRegs.RemoveObject(c); otlvRegs.RefreshObject(p); }
The code you have works for root level objects (as you've noted), but for childrem nodes,
RemoveObject()
isn't doing what you think.For children nodes, you need to change your model. As your code stands,
Node.Children
is unchanged so yourChildrenGetter
will still return the same collection.You'd need to do something like:
That's much more complex than I'd like.
It's complex because root objects are under your direct control (hence
RemoveObject
works), but children are controlled by what's in your model -- so you need to modify your model to change what's in the tree.