Reputation: 155
I have two CSV files which I merge together, create a recursive list, then use that data as the source for a TreeView.
The first file has three fields [Id; Level; Description] and looks like this:
The second file has three fields [Id; Description; Wage] and looks like this:
Before merging the TreeView Looks like this:
The colors are there to make it easier to understand what is being displayed.
What I'm doing is inserting the data of the second file, using the provided Id as the insertion id so I know where to insert the item, accumulating the values for that item and displaying that accumulated value in the items own row. This is what it looks like after merging the data:
As you can see, the three items have been inserted at the correct position and the parent of the inserted items has the accumulated value (e.g. Id 1121 displays 200,00, Id 1123 displays 100,00) but it is not complete, as I need the values to be accumulated all the way up the TreeView!
This is what I'm hoping to achieve (some values have been hard-coded);
I'm guessing that after inserting the items at any given position and accumulating the totals for that parent, I need to go up the tree, adding the parents total to each of its parents until I get to the root item - I simply have no idea as how to accomplish this!
I hope that you gurus can see that I have put a little effort into this and have a little time to help me. Your time is, as always, very much appreciated.
Have a great day!
EDIT
Thank you all for pointing out that I should actually show some code and not only what I have accomplished until now - sorry for that!
Here is where I'm merging the data:
IndentedData is the TreeView's data source.
MergedData is the data contained in the second file.
As suggested by Peter Duniho, here is the actual code and not a screenshot.
/*
* Merge the data
*/
TransformedData target = null;
MergeData mergeData = null;
var groupedMergedData = this.MergedData.GroupBy(x => x.Position).ToList();
foreach (var group in groupedMergedData)
{
var groupTarget = group.FirstOrDefault();
if (groupTarget != null)
{
target = this.IndentedData.FirstOrDefaultFromMany(x => x.Children, x => x.Id == groupTarget.Position);
if (target != null)
{
double d = 0;
for (int i = 0; i < group.Count(); i++)
{
mergeData = group.ElementAt(i);
double.TryParse(mergeData.YearCurrent, out d);
target.Children.Insert(i, new TransformedData()
{
Id = mergeData.Account,
Description = string.Format("{0} {1}", mergeData.Account, mergeData.Description),
CurrentYear = d
});
target.CurrentYear += d;
}
}
}
}
As suggested by Servy, here are what the two classes look like:
public class MergeData
{
public string Position { get; set; }
public string Account { get; set; }
public string Description { get; set; }
public string YearCurrent { get; set; }
}
public class TransformedData
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Description { get; set; }
public double CurrentYear { get; set; }
public List<TransformedData> Children { get; set; }
public TransformedData()
{
this.Children = new List<TransformedData>();
}
}
Upvotes: 0
Views: 373
Reputation: 203827
You get the sum of the value for not only the item itself, but also all of it's children, just add a property that adds the current items value to the computed value of all of its children:
public class TransformedData
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Description { get; set; }
public double CurrentYear { get; set; }
public double RenameThisProperty
{
get
{
return CurrentYear + Children.Sum(child => child.RenameThisProperty);
}
}
public List<TransformedData> Children { get; set; }
public TransformedData()
{
this.Children = new List<TransformedData>();
}
}
Upvotes: 1