user3866053
user3866053

Reputation: 21

How Do I Select Multiple Nodes At A Time From Treeview Control Using C# And Visual Studio 2013

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 with checkbox and nodes 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:\DNP\Opening ppt\Opening ppt\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();
    }

Upvotes: 0

Views: 5673

Answers (3)

user3866053
user3866053

Reputation: 21

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;
                }
            }

        }

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); } }

Upvotes: 1

Robert Hegner
Robert Hegner

Reputation: 9376

I agree with nevets - it's a real pain to do multiple selection with the TreeView control.

In my own projects I use ObjectListView instead. I use it as a replacement for the WinForm ListView and TreeView controls. It supports multiple selection, tree views with multiple columns, and much more!

Upvotes: 0

nevets
nevets

Reputation: 4818

This question has been asked several times, and the answer is: it's possible, but not trivial.

Native TreeView control does not allow multiple selection, but you could derive a subclass from it and override some behaviors to make it work.

Personally I don't recommend this, since it's not easy to get it right. I suggest you search some controls that may do the job.

Please check this site, it gives a TreeView control that allows multiple selection, and technical details as well :)

Upvotes: 0

Related Questions