在与我的第一个TreeView挣扎之后,它几乎可以工作。我的问题是在Click事件中,在代码的底部,根节点似乎是集合中的唯一节点,这阻止我在检查根节点时检查所有节点。虽然我怀疑我可能错误地添加了节点,但我不知道我做了什么导致这种情况。 树中有一个根节点和12个parentNode节点。每个parentNode都有多个secondChild节点。每个secondChild节点都有多个thirdChild节点。这是一个Windows窗体。这里列出了所有代码。任何帮助表示赞赏。
public Form1()
{
InitializeComponent();
FillTreeParentNodes();
}
public void FillTreeParentNodes()
{
connect.Open();
DataTable dtGroups = new DataTable("FirstChildNodes");
DataSet dsNodes = new DataSet();
dsNodes.Tables.Add(dtGroups);
SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorDesc", connect);
daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
daAdapter.Fill(dsNodes, "FirstChildNodes");
daAdapter.Dispose();
tvDiscountMaintenance.Nodes.Clear();
if (dsNodes.Tables[0].Rows.Count > 0)
{
TreeNode root = new TreeNode("Select All");
tvDiscountMaintenance.Nodes.Add(root);
foreach (DataRow row in dsNodes.Tables[0].Rows)
{
TreeNode parentNode = new TreeNode(row["DisColorDesc"].ToString());
parentNode.Text = row["DisColorDesc"].ToString();
root.Nodes.Add(parentNode);
FillChildNodes(parentNode, root);
}
}
}
private void FillChildNodes(TreeNode parentNode, TreeNode root)
{
DataTable dtSecondGroup = new DataTable("SecondChildNodes");
DataSet dsCNodes = new DataSet();
dsCNodes.Tables.Add(dtSecondGroup);
SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVColorCodes", connect);
daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
daAdapter.Fill(dsCNodes, "SecondChildNodes");
daAdapter.Dispose();
if (dsCNodes.Tables[0].Rows.Count > 0)
{
foreach (DataRow chRow in dsCNodes.Tables[0].Rows)
{
if (parentNode.Text.Equals(chRow["DisColorDesc"].ToString()))
{
TreeNode secondChild = new TreeNode();
secondChild.Text = chRow["DisColorCode"].ToString();
parentNode.Nodes.Add(secondChild);
FillThirdChildNodes(secondChild, root);
}
}
}
}
private void FillThirdChildNodes(TreeNode secondChild, TreeNode root)
{
DataTable dtThirdGroup = new DataTable("ThirdChildNodes");
DataSet dsThNodes = new DataSet();
dsThNodes.Tables.Add(dtThirdGroup);
SqlDataAdapter daAdapter = new SqlDataAdapter("RMM3DMTVCategories", connect);
daAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
daAdapter.Fill(dsThNodes, "ThirdChildNodes");
daAdapter.Dispose();
if (dsThNodes.Tables[0].Rows.Count > 0)
{
foreach (DataRow chRow in dsThNodes.Tables[0].Rows)
{
if (secondChild.Text.Equals(chRow["DisColorCode"].ToString()))
{
TreeNode thirdChild = new TreeNode();
thirdChild.Text = chRow["DisCategoryDesc"].ToString;
secondChild.Nodes.Add(thirdChild);
}
}
}
connect.Close();
}
private void tvDiscountMaintenance_Click(object sender, EventArgs e)
{
if (tvDiscountMaintenance.Nodes.Count > 0) // I think this is telling me that the root node is the only one in the Collection.
{
if (tvDiscountMaintenance.TopNode.Checked)
{
foreach (TreeNode node in tvDiscountMaintenance.Nodes)
{
node.ExpandAll();
node.Checked = true;
}
}
}
}
答案 0 :(得分:0)
树视图的节点集合仅告诉您此集合中存在多少个节点,您将子节点添加到其他节点,因此要查看总共存在多少个节点,您必须遍历节点树。
如果您只想检查根节点内的节点,可以使用tvDiscountMaintenance.Nodes[0].Nodes