如何在 parent c# dataTreeListView 中显示 chid 值的总和
How to show sum of values of chid in parent c# dataTreeListView
我可以知道如何在 c# dataTreeListView
中显示 parent 中 child 的总和吗
我分享了一个屏幕截图
我想知道如何向 A 显示 5000,它是 child table 和 parent table 的总和 任何人都可以帮助我
我想在 c# object listview
上执行此操作
我为了爬上树所做的是
在表单加载时我写了这段代码
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");
和
class1.cs 文件有
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;
}
}
}
请帮我做这个
在问题的标签部分我添加了 objectlistview 但我想显示详细信息
数据树列表视图
您可以使用 linq 按父级筛选,然后对列表求和,如下所示:
public static float SumChildren(string parentKey){
return GetList().Where(x => x.ParentId == parentKey).Sum(x => x.Debit)
}
您可能需要切换 return 类型,但我假设 Debit 是小数
根据您的评论,您的代码需要更大的重构。您应该通过一种方法添加数据,并让该方法根据添加的子项更新父项借记值。
但是作为快速破解,您可以只分配父值,但是如果您要为多嵌套树执行此操作,则需要从最深的分支开始并逐步向上。
var parentDebitValue = SumChildren("xyz");
GetList().Where(x => x.Id == "xyz").First().Debit = parentDebitValue;
但实际上你应该重新构建你的程序
我可以知道如何在 c# dataTreeListView
中显示 parent 中 child 的总和吗我分享了一个屏幕截图
我想知道如何向 A 显示 5000,它是 child table 和 parent table 的总和 任何人都可以帮助我
我想在 c# object listview
上执行此操作我为了爬上树所做的是
在表单加载时我写了这段代码
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");
和
class1.cs 文件有
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;
}
}
}
请帮我做这个
在问题的标签部分我添加了 objectlistview 但我想显示详细信息 数据树列表视图
您可以使用 linq 按父级筛选,然后对列表求和,如下所示:
public static float SumChildren(string parentKey){
return GetList().Where(x => x.ParentId == parentKey).Sum(x => x.Debit)
}
您可能需要切换 return 类型,但我假设 Debit 是小数
根据您的评论,您的代码需要更大的重构。您应该通过一种方法添加数据,并让该方法根据添加的子项更新父项借记值。
但是作为快速破解,您可以只分配父值,但是如果您要为多嵌套树执行此操作,则需要从最深的分支开始并逐步向上。
var parentDebitValue = SumChildren("xyz");
GetList().Where(x => x.Id == "xyz").First().Debit = parentDebitValue;
但实际上你应该重新构建你的程序