Hi @Farshad Valizade , Welcome to Microsoft Q&A,
The AddNode
method is used to add nodes to the tree view, but its design causes the search to always start from the root node when adding child nodes, and stop when the first matching node is found. This will cause problems in a deep structure.
Try this code and let me know if I understand it wrong:
private void comboLineno_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboLineno.SelectedIndex > -1)
{
int.TryParse(comboLineno.SelectedValue.ToString(), out int lineId);
if (lineId == 0)
return;
treeView1.Nodes.Clear();
var data = JointBLL.LineJointsQcHistory(lineId);
if (data != null)
{
var _enumrableData = data.AsEnumerable();
TreeNode rootNode = new TreeNode(comboLineno.Text);
treeView1.Nodes.Add(rootNode);
rootNode.ImageIndex = 0;
rootNode.SelectedImageIndex = 0;
foreach (var item in _enumrableData.Select(r => r.Field<string>("JointNo")).Distinct())
{
TreeNode jointNode = new TreeNode(item);
rootNode.Nodes.Add(jointNode);
jointNode.ImageIndex = 1;
jointNode.SelectedImageIndex = 1;
foreach (var kind in _enumrableData.Where(r => r.Field<string>("JointNo") == item).Select(r => r.Field<string>("Kind")).Distinct())
{
TreeNode kindNode = new TreeNode(kind);
jointNode.Nodes.Add(kindNode);
kindNode.ImageIndex = 2;
kindNode.SelectedImageIndex = 2;
foreach (var report in _enumrableData.Where(r => r.Field<string>("JointNo") == item && r.Field<string>("Kind") == kind))
{
TreeNode reportNode = new TreeNode(report.Field<string>("ReportNo"));
kindNode.Nodes.Add(reportNode);
reportNode.ImageIndex = 3;
reportNode.SelectedImageIndex = 3;
}
}
}
treeView1.ExpandAll();
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.