I created my treeview without using ID or PARENTID by Infragistics Framework. It worked great.
However sometimes childnode names are become same. Which is okay for me.
Here is the code that i populatetreeview. Basically group by for 'Type'. Which are root nodes. Then add child nodes for that root nodes using 'Name'
private void populateTreeview(DataTable dt)
{
var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Type"));
foreach (var group in groups)
{
UltraTreeNode node = utObjects.Nodes.Add(group.Key);
foreach (string name in group.Select(x => x.Field<string>("Name")))
{
node.Nodes.Add(name); // Throws an error here : 'Key Already Exist'
}
}
}
How can I allow duplicate keys ?
You can't allow duplicate keys.
According to the Key
property documentation:
Keys must be unique throughout the UltraTree control.
The documentation for the Add
method shows 5 overloads.
One of them accepts two strings - first is key and the other is text.
You should use this overload to add your nodes to the tree.
You can choose whatever method you want to set the key, I think the simplest one that will have the smallest effect on your current code would be to use Guid.NewGuid().ToString()
for the key. Guids are practically guaranteed to be unique:
private void populateTreeview(DataTable dt)
{
var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Type"));
foreach (var group in groups)
{
UltraTreeNode node = utObjects.Nodes.Add(group.Key);
foreach (string name in group.Select(x => x.Field<string>("Name")))
{
// Generates a unique key for each node.
node.Nodes.Add(Guid.NewGuid().ToString(), name);
}
}
}