Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to select the multiple nodes from a treeview and want its text to be present in msgbox or console . but i am not able to do so. can anybody please help me.?

Added from answer below
C#
//Create the tree from the database
        private void CreateTree(TreeNode n, int hdrID)
        {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Builder.mdf;MultipleActiveResultSets = True;Integrated Security=True;Connect Timeout=30");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT Id,Title FROM Presentation WHERE Pid=" + hdrID, con);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                TreeNode t = new TreeNode(rdr["Title"].ToString());
                CreateTree(t, Convert.ToInt16(rdr["Id"].ToString()));
                if (n == null)
                {
                    treeView1.Nodes.Add(t);
                    //t.ImageIndex = 0;
                }
                else
                {
                    n.Nodes.Add(t);
                }
            }
            rdr.Close();
        }
Posted
Updated 28-Aug-14 4:03am
v2
Comments
_Amy 25-Aug-14 5:30am    
So? Where is the error? Can I see your code?
nits23 25-Aug-14 7:24am    
_Amy i want to select multiple field at once in treeview , at a time i am able to select only a single node. can you help me with this?
SteveyJDay 25-Aug-14 9:19am    
You need to post your current code. We can't see your screen and we aren't going to work up a solution from scratch.
nits23 26-Aug-14 1:30am    
@jaredSanow and @_Amy sorry for the inconvenience, Here are the code for creating the tree structure from the database in the treeview control of windows forms and my table contains Id , Title , Pid , DescId attributes. and yes i am calling this method from inside a button click event like this only:- CreateTree(null, 0);

Property of treeview i already made checkboxes==true so by this when tree populate they came together checkboxe and node of the treeview.

//Create the tree from the database
private void CreateTree(TreeNode n, int hdrID)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Builder.mdf;MultipleActiveResultSets = True;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Id,Title FROM Presentation WHERE Pid=" + hdrID, con);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
TreeNode t = new TreeNode(rdr["Title"].ToString());
CreateTree(t, Convert.ToInt16(rdr["Id"].ToString()));
if (n == null)
{
treeView1.Nodes.Add(t);
//t.ImageIndex = 0;
}
else
{
n.Nodes.Add(t);
}
}
rdr.Close();
}


If you don't want checkboxes next to each item, this may be of use to you.

http://www.codeproject.com/Articles/8006/Multi-select-Treeview-control-v
 
Share this answer
 
C#
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                 if (busy) return;
                busy = true;
                try
                {
                    TreeNode _node = e.Node;
                    
                    checkNodes(e.Node, e.Node.Checked);
                    if (e.Node.Checked)
                    {
                        MessageBox.Show(e.Node.Text);
                    }
                }
                
               
                finally
                {
                    busy = false;
                }
            }

        }

C#
private void checkNodes(TreeNode node, bool check)
        {
            foreach (TreeNode child in node.Nodes)
            {
                if (child.Checked == true)
                {
                    MessageBox.Show(child.Text);
                } 
                //MessageBox.Show(child.Text);
                checkNodes(child, check);
            }
        }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900