最初のTreeViewに苦労した後、ほとんど動作します。私の問題は、Clickイベントのコードの一番下に、ルートノードがコレクションの唯一のノードであるように見えて、ルートノードがチェックされたときにすべてのノードをチェックできなくなってしまうことです。私はおそらくノードを間違って追加したと思われるが、これを引き起こすために何をしたのか分からない。 ツリーにはルートノードと12個のparentNodeノードがあります。各parentNodeには複数のsecondChildノードがあります。各secondChildノードには複数のthirdChildノードがあります。これはWindowsフォームです。すべてのコードがここにリストされています。どんな助けもありがとうございます。子ノードはTreeViewコレクションにありません
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;
}
}
}
}
に確認することができます。再帰的に行われたベスト。 – TaW
ありがとう。前の質問や例が分かっていますか?私はここで私の要素の方法です。 – mengli00
[はい。 TreeViewですべてのノードを収集する関数については、ここを参照](http://stackoverflow.com/questions/26687906/find-only-child-nodes-in-treeview-c-sharp/26687949?s=6|1.8069# 26687949) – TaW