gpt4 book ai didi

c# - 将对象分配给c#中的treeview子节点以识别父节点

转载 作者:行者123 更新时间:2023-11-30 16:46:35 25 4
gpt4 key购买 nike

您好,我正在尝试根据它们的字符串是否匹配将子节点附加到父节点。

我遇到了一些问题,因为我不确定如何仅通过名字来识别 parent 。例如,如果 "string a"== "string a"那么我想将子节点 LoanName 添加到字符串与父名称匹配的父 id。

如果我这样做,这会起作用:

tvTodoList.Nodes[0].Nodes.Add(activityResult.ActivityName);

但显然这会将子节点附加到 TreeView 中的第一个父节点。如何让它与变量 loanresult.LoanName 的名称相匹配?

下面是我的 FillTodoList 方法的代码

private void FillTodoList()
{
var nol = NetworkOpsLayer.NetworkOpsLayer.CreateForDirectMongoConnection("mongodb://localhost", "test", "loans");
//demoSave(nol);

var loanList = nol.GetDocsWhichMatchGivenDocString("{ \"isActive\" : 1 }");

foreach (string s in loanList)
{
//System.Console.WriteLine(s);
var loanResult = JsonConvert.DeserializeObject<RootObject>(s);

tvTodoList.Nodes.Add("Loan Name: " + loanResult.LoanName);

//Add children to each Loan

var con = NetworkOpsLayer.NetworkOpsLayer.CreateForDirectMongoConnection("mongodb://localhost", "test", "activity");
//demoSave(con);
var activityList = con.GetDocsWhichMatchGivenDocString("{ \"isActive\" : 1 }");

foreach (string st in activityList)
{
var activityResult = JsonConvert.DeserializeObject<Activity>(st);
if (loanResult.LoanName == activityResult.ParentLoanName)
{
tvTodoList.Nodes[loanResult.LoanName].Nodes.Add(activityResult.ActivityName);
}
}
}
}

最佳答案

方法 TreeNodeCollection.Find(string, boolean) 将对此有所帮助...

foreach (string st in activityList)
{
var activityResult = JsonConvert.DeserializeObject<Activity>(st);
if (loanResult.LoanName == activityResult.ParentLoanName)
{
TreeNode[] matches = tvTodoList.Nodes.Find("Loan Name: " + loanResult.LoanName, false);
if (matches.Length > 0) matches[0].Nodes.Add(activityResult.ActivityName);
}
}

关于c# - 将对象分配给c#中的treeview子节点以识别父节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com