Quote:
... can someone please explain how exactly that Tag works ?
Well, i'd suggest to read the documentation:
Control.Tag Property (System.Windows.Forms) | Microsoft Docs[
^]
TreeView Class (System.Windows.Forms) | Microsoft Docs[
^]
As to your data structure and treview...
Usually, treview component is used to display hierarchical data, but i do not see any reference to previous/next object.
I agree with
Chris Copeland[
^] (solution#2) that you have to use custom
class
.
So, your struct
struct myRecordStruct{
double data1
double data2
double data3
List<double> dataList
}
can be "converted" to:
public class myRecord
{
public double data1 {get; set;}
public double data2 {get; set;}
public double data3 {get; set;}
public List<myRecord> dataList {get; set;}
public override string ToString()
{
return $"myRecord: {{{data1.ToString("F2")} | {data2.ToString("F2")} | {data3.ToString("F2")}}}";
}
}
Then, you'll be able to create a list of
myRecord
with any level\depth of sub-myRecords. :)
So, final procedure/method to add nodes may look like:
private void LoadInitialData()
{
foreach(myRecord mr in data)
{
TreeNode root = null;
PopulateNode3(ref root, mr);
oTV.Nodes.Add(root);
}
oTV.ExpandAll();
}
private void PopulateNode(ref TreeNode root, myRecord record)
{
root = new TreeNode("myRecord");
TreeNode subnode1 = new TreeNode(record.data1.ToString());
TreeNode subnode2 = new TreeNode(record.data2.ToString());
TreeNode subnode3 = new TreeNode(record.data3.ToString());
TreeNode subnode4 = new TreeNode("dataList");
root.Nodes.AddRange(new TreeNode[]{subnode1, subnode2, subnode3, subnode4});
if(record.dataList!=null)
{
foreach(myRecord mr in record.dataList)
{
TreeNode childnode = null;
PopulateNode(ref childnode, mr);
subnode4.Nodes.Add(childnode);
}
}
}
private void PopulateNode2(ref TreeNode root, myRecord record)
{
root = new TreeNode(record.ToString());
TreeNode subnode1 = new TreeNode("dataList");
root.Nodes.Add(subnode1);
if(record.dataList!=null)
{
foreach(myRecord mr in record.dataList)
{
TreeNode childnode = null;
PopulateNode2(ref childnode, mr);
subnode1.Nodes.Add(childnode);
}
}
}
private void PopulateNode3(ref TreeNode root, myRecord record)
{
root = new TreeNode(record.ToString());
if(record.dataList!=null)
{
foreach(myRecord mr in record.dataList)
{
TreeNode childnode = null;
PopulateNode3(ref childnode, mr);
root.Nodes.Add(childnode);
}
}
}
The way of data presentation depends on you.
Note: truly doubt that you need to use
Tag
property. Get selected node, find
myRecord
on the list by using node/subnodes. Change object properties and update selected node.
TreeNode node = oTV.SelectedNode;
if(node!=null)
{
myRecord mr = data.Where(x=>x.data1 = node.Nodes[0].Text && x.data2 = node.Nodes[1].Text && x.data3 = node.Nodes[2].Text).SingleorDefault();
mr.data1 = 8888;
node.Nodes[0] = data1;
}
See also:
How to: Determine Which TreeView Node Was Clicked - Windows Forms .NET Framework | Microsoft Docs[
^]