may i know how to show sum of the child in parent in c# dataTreeListView
i wish to know how to show 5000 in person A which is sum of child table and the parent table can any one please help me
i want to do this on c# object listview
what i done to get above tree is
on form load i wrote this code
List<Class1> list = new List<Class1>();
list = Class1.GetList();
this.dataTreeListView2.ParentKeyAspectName = "ParentId";
this.dataTreeListView2.RootKeyValueString = "1";
BrightIdeasSoftware.OLVColumn col = new BrightIdeasSoftware.OLVColumn();
col.Width = 10;
dataTreeListView2.DataSource = list;
foreach (ColumnHeader column in this.dataTreeListView2.Columns)
{
column.Width = 100 + 100+ this.dataTreeListView2.SmallImageSize.Width;
}
this.dataTreeListView2.Columns.RemoveByKey("Id");
this.dataTreeListView2.Columns.RemoveByKey("ParentId");
and
class1.cs file has
namespace bbcon_accout_software
{
class Class1
{
/*public string Name { get; set;}
public Double Value { get; set; }
public List<Double> Children {get; set;}*/
protected string xName;
protected string xId;
protected string xParentId;
protected decimal xcredit;
protected decimal xdebit;
public Class1()
{
//Name = name;
//Value = value;
//Children = new List<Double>();
//Children.Add(2);
//Children.Add(3);
this.xName = "";
this.xId = "";
this.xParentId = "";
this.xcredit = 0.0M;
this.xdebit = 0.0M;
}
public String Name1
{
get { return this.xName; }
set { this.xName = value; }
}
public String Id
{
get { return this.xId; }
set { this.xId = value; }
}
public String ParentId
{
get { return this.xParentId; }
set { this.xParentId = value; }
}
public Decimal Credit
{
get { return this.xcredit; }
set { this.xcredit = value; }
}
public Decimal Debit
{
get { return this.xdebit; }
set { this.xdebit = value; }
}
public static List<Class1> GetList()
{
List<Class1> oList = new List<Class1>();
Class1 oClass4 = new Class1();
oClass4.Name1 = "Person A";
oClass4.Id = "xyz";
oClass4.ParentId = "1";
oClass4.Credit = 1;
oClass4.Debit = 1000;
oList.Add(oClass4);
Class1 oClass1 = new Class1();
oClass1.Name1 = "Person B";
oClass1.Id = "ldc";
oClass1.ParentId = "xyz";
oClass1.Credit = 1;
oClass1.Debit = 2000;
oList.Add(oClass1);
Class1 oClass2 = new Class1();
oClass2.Name1 = "Person C";
oClass2.Id = "ccc";
oClass2.ParentId = "xyz";
oClass2.Credit = 1;
oClass2.Debit = 1000;
oList.Add(oClass2);
Class1 oClass5 = new Class1();
oClass5.Name1 = "Person A";
oClass5.Id = "mno";
oClass5.ParentId = "xyz";
oClass5.Credit = 1;
oClass5.Debit = 1000;
oList.Add(oClass5);
return oList;
}
}
}
please help me to do this
in tag section of question i added objectlistview but i want to show details on dataTreeListview
You can use linq to filter by the parent and then sum the list like so:
public static float SumChildren(string parentKey){
return GetList().Where(x => x.ParentId == parentKey).Sum(x => x.Debit)
}
You may have to switch the return type, but I'm assuming Debit is a decimal
Based on your comments your code requires a bigger refactor. You should be adding the data through a method and have that method update the parent debit value based off of the children added.
But As a quick hack you can just assign the parent value, but if you're going to do this for multi-nest trees you'll need to start at the deepest branch and work your way up.
var parentDebitValue = SumChildren("xyz");
GetList().Where(x => x.Id == "xyz").First().Debit = parentDebitValue;
But realisticly you should re achitect your program