Reputation: 2626
Hi all I am creating a tree-view dynamically at run-time. My Tree view is below. When I check the Master(Parent) The Child Register Masters and Class Masters want to Checked Automatically.
But I can't code Node4.checked = true like this. Because I have created this treeview in runtime.
Please give any valuable suggestion.
UpDated Question -
private void MenuRights_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("data");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("menu_name");
dt.Columns.Add("parentid", typeof(int));
dt.Columns.Add("menu_key");
hp.getConnStr();
MySqlConnection connection = new MySqlConnection(hp.myConnStr);
MySqlCommand command = new MySqlCommand("select * from mcs_menu_details", connection);
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
while (Reader.Read())
{
//dt.Rows.Add(new string[] { Reader[0].ToString(), Reader[1].ToString(), Reader[2].ToString(), Reader[3].ToString() });
DataRow row = dt.NewRow();
row["id"] = Convert.ToInt32(Reader[0].ToString());
row["menu_name"] = Reader[1].ToString();
if (Reader[2].ToString() != Convert.ToString(0))
{
row["parentid"] = Convert.ToInt32(Reader[2].ToString());
}
else { row["parentid"] = DBNull.Value; }
row["menu_key"] = Reader[3].ToString();
dt.Rows.Add(row);
}
connection.Close();
//Use a DataSet to manage the data
DataSet ds = new DataSet();
ds.Tables.Add(dt);
//add a relationship
ds.Relations.Add("rsParentChild", ds.Tables["data"].Columns["id"],ds.Tables["data"].Columns["parentid"]);
foreach (DataRow dr in ds.Tables["data"].Rows)
{
if (dr["parentid"] == DBNull.Value)
{
TreeNode root = new TreeNode(dr["menu_name"].ToString());
root.Tag = dr["menu_key"].ToString();
treeView1.Nodes.Add(root);
PopulateTree(dr, root);
}
}
treeView1.ExpandAll();
}
public void PopulateTree(DataRow dr, TreeNode pNode)
{
foreach (DataRow row in dr.GetChildRows("rsParentChild"))
{
TreeNode cChild = new TreeNode(row["menu_name"].ToString());
cChild.Tag = row["menu_key"].ToString();
pNode.Nodes.Add(cChild);
//Recursively build the tree
PopulateTree(row, cChild);
}
}
The Above Code is help me to create the treeview. This may given you a clear view of my question.
Thanks in advance.
Upvotes: 0
Views: 3403
Reputation: 224
I've solved this method without recursion, put this code in AfterCheck
event.
if (e.Node.FirstNode != null)
{
TreeNode tn = e.Node.FirstNode;
while (tn != null) // check all childrens.
{
tn.Checked = e.Node.Checked;
tn = tn.NextNode;
}
}
Upvotes: 0
Reputation: 49013
Note sure to fully understand what you're asking here....
If you're creating your treeview dynamically, I assume you're adding nodes using Nodes.Add(...)
. Just specifies Checked
as true
to check your inserted node.
private void MainForm2_Load(object sender, EventArgs e)
{
TreeNode newNode = new TreeNode
{
Text = "Mytext",
Checked = true
};
this.treeView1.Nodes.Add(newNode);
}
If you want to check your child nodes at runtime, just do some recursion like this:
private void button1_Click(object sender, EventArgs e)
{
if (this.treeView1.SelectedNode != null)
{
this.CheckChildrens(this.treeView1.SelectedNode);
}
}
private void CheckChildrens(TreeNode node)
{
node.Checked = true;
foreach (TreeNode child in node.Nodes)
{
this.CheckChildrens(child);
}
}
But don't call directly CheckChildrens(...)
without caution from AfterCheck
event handler (if you want to take the new checked node into account) or you'll have a nice StackOverflow
exception.
Upvotes: 2
Reputation: 30127
You can simply iterate through the tree to check each node
Let's suppose node is the TreeNode
in tree which is checked and you want to check all the children of this TreeNode
CheckAllChildren(TreeNode node)
{
foreach(TreeNode child in node.Nodes)
{
child.Checked = true;
CheckAllChildren(child);
}
}
Upvotes: 2