Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Drag and Drop Treeview Item into listview

Drag and Drop Treeview Item into listview

Scheduled Pinned Locked Moved C#
tutorialhelpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Udayaraju
    wrote on last edited by
    #1

    :confused:Hi EveryBody, How to Drag and Drop Treeview Item into listview? Please help me out.. I have one parent node and two child nodes. when I drag a Parent Node, included child nodes are also to be dragged. And all of them to be placed in listview row (3 columns). Please help me out with a sample example. Itz very urgent for me:confused::confused:

    U D 2 Replies Last reply
    0
    • U Udayaraju

      :confused:Hi EveryBody, How to Drag and Drop Treeview Item into listview? Please help me out.. I have one parent node and two child nodes. when I drag a Parent Node, included child nodes are also to be dragged. And all of them to be placed in listview row (3 columns). Please help me out with a sample example. Itz very urgent for me:confused::confused:

      U Offline
      U Offline
      Udayaraju
      wrote on last edited by
      #2

      Please reply me

      1 Reply Last reply
      0
      • U Udayaraju

        :confused:Hi EveryBody, How to Drag and Drop Treeview Item into listview? Please help me out.. I have one parent node and two child nodes. when I drag a Parent Node, included child nodes are also to be dragged. And all of them to be placed in listview row (3 columns). Please help me out with a sample example. Itz very urgent for me:confused::confused:

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Udayaraju wrote:

        Itz very urgent for me

        ... but not urgent for us. I saw your post hours ago but ignored it because of this. Bumping your post will not get you any further. I won't give you the code but I'll give you the theory so you can code it yourself. If you get stuck with a specific part of your code, post it and we'll have a look. What you want is a very simple 3 step process. In the treeview's mousedown event , get the node at the mouse point. Set the selectednode to be that node If it's not null and is a 'Parent' node, call the treeview's DoDragDrop method. In the listview's dragover event, set the effect property of the event arguments to copy. In the litview's DragDrop event, convert the data to a treenode (would be a good idea to null check here too) then convert it and it's nodes (children) to a listviewitem with listviewsubitems and add to your listview.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

        U 1 Reply Last reply
        0
        • D DaveyM69

          Udayaraju wrote:

          Itz very urgent for me

          ... but not urgent for us. I saw your post hours ago but ignored it because of this. Bumping your post will not get you any further. I won't give you the code but I'll give you the theory so you can code it yourself. If you get stuck with a specific part of your code, post it and we'll have a look. What you want is a very simple 3 step process. In the treeview's mousedown event , get the node at the mouse point. Set the selectednode to be that node If it's not null and is a 'Parent' node, call the treeview's DoDragDrop method. In the listview's dragover event, set the effect property of the event arguments to copy. In the litview's DragDrop event, convert the data to a treenode (would be a good idea to null check here too) then convert it and it's nodes (children) to a listviewitem with listviewsubitems and add to your listview.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          U Offline
          U Offline
          Udayaraju
          wrote on last edited by
          #4

          Hi, Thanq for ur Suggestion But i tried inthes way..can you corect the problem here. See the below code. Here I have a Problem. Treeview has already one root node and twochild nodes with it. They have to be dragged and dropped in listview. The main Problem is wen I drag an item..the Itemdrag event is not fired.?? Please try to resolve it. private void tvwSalesProducts_ItemDrag(object sender, ItemDragEventArgs e) { tvwSalesProducts.DoDragDrop(e.Item, DragDropEffects.Copy | DragDropEffects.Move); } private void lstMultiProItems_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy | DragDropEffects.Move; } private void lstMultiProItems_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { string[] Items = new string[5]; TreeNode SourceNode; SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); Items[0] = Convert.ToString(SourceNode.Tag); Items[1] = SourceNode.Text.ToString(); Items[2] = SourceNode.FirstNode.Text.ToString(); Items[3] = SourceNode.LastNode.Text.ToString(); lstMultiProItems.Items.Clear(); lstMultiProItems.Columns.Add("ID", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Product Name", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Selling Price", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Conversion", 100, HorizontalAlignment.Left); ListViewItem Item1 = new ListViewItem(Items); lstMultiProItems.Items.Add(Item1); lstMultiProItems.AllowColumnReorder = false; lstMultiProItems.FullRowSelect = true; } }

          D U 2 Replies Last reply
          0
          • U Udayaraju

            Hi, Thanq for ur Suggestion But i tried inthes way..can you corect the problem here. See the below code. Here I have a Problem. Treeview has already one root node and twochild nodes with it. They have to be dragged and dropped in listview. The main Problem is wen I drag an item..the Itemdrag event is not fired.?? Please try to resolve it. private void tvwSalesProducts_ItemDrag(object sender, ItemDragEventArgs e) { tvwSalesProducts.DoDragDrop(e.Item, DragDropEffects.Copy | DragDropEffects.Move); } private void lstMultiProItems_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy | DragDropEffects.Move; } private void lstMultiProItems_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { string[] Items = new string[5]; TreeNode SourceNode; SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); Items[0] = Convert.ToString(SourceNode.Tag); Items[1] = SourceNode.Text.ToString(); Items[2] = SourceNode.FirstNode.Text.ToString(); Items[3] = SourceNode.LastNode.Text.ToString(); lstMultiProItems.Items.Clear(); lstMultiProItems.Columns.Add("ID", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Product Name", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Selling Price", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Conversion", 100, HorizontalAlignment.Left); ListViewItem Item1 = new ListViewItem(Items); lstMultiProItems.Items.Add(Item1); lstMultiProItems.AllowColumnReorder = false; lstMultiProItems.FullRowSelect = true; } }

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Check out the working code below and compare - you should be able to figure it out from this.

            /\* Assumes
            TreeView treeView1. Parent node must have Tag "Parent" and
            exactly two child nodes for drag to work
            ListView listView1 with three columns already added.
            
            Events subscribed
            treeView1.ItemDrag += new ItemDragEventHandler(treeView1\_ItemDrag);
            listView1.DragOver += new DragEventHandler(listView1\_DragOver);
            listView1.DragDrop += new DragEventHandler(listView1\_DragDrop);
            \*/
            void treeView1\_ItemDrag(object sender, ItemDragEventArgs e)
            {
                TreeNode nodeData = (TreeNode)e.Item;
                // set as selected
                treeView1.SelectedNode = nodeData;
                // check is parent using tag property
                if ((string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                    // OK
                    treeView1.DoDragDrop(nodeData, DragDropEffects.Copy);
            }
            void listView1\_DragOver(object sender, DragEventArgs e)
            {
                // default to none
                e.Effect = DragDropEffects.None;
                // get tree node
                TreeNode nodeData = (TreeNode)e.Data.GetData(typeof(TreeNode));
                // check data in case coming from elsewhere
                if (nodeData != null && (string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                    // OK so proceed
                    e.Effect = DragDropEffects.Copy;
            
            }
            void listView1\_DragDrop(object sender, DragEventArgs e)
            {
                // get tree node
                TreeNode nodeData = (TreeNode)e.Data.GetData(typeof(TreeNode));
                // check data
                if (nodeData != null && (string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                {
                    // convert
                    ListViewItem newItem = new ListViewItem(nodeData.Text);
                    foreach (TreeNode childNode in nodeData.Nodes)
                    {
                        ListViewItem.ListViewSubItem newSub = new ListViewItem.ListViewSubItem();
                        newSub.Text = childNode.Text;
                        // add children to parent
                        newItem.SubItems.Add(newSub);
                    }
                    // add parent
                    listView1.Items.Add(newItem);
                }
            }
            

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is n

            U 1 Reply Last reply
            0
            • U Udayaraju

              Hi, Thanq for ur Suggestion But i tried inthes way..can you corect the problem here. See the below code. Here I have a Problem. Treeview has already one root node and twochild nodes with it. They have to be dragged and dropped in listview. The main Problem is wen I drag an item..the Itemdrag event is not fired.?? Please try to resolve it. private void tvwSalesProducts_ItemDrag(object sender, ItemDragEventArgs e) { tvwSalesProducts.DoDragDrop(e.Item, DragDropEffects.Copy | DragDropEffects.Move); } private void lstMultiProItems_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy | DragDropEffects.Move; } private void lstMultiProItems_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) { string[] Items = new string[5]; TreeNode SourceNode; SourceNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); Items[0] = Convert.ToString(SourceNode.Tag); Items[1] = SourceNode.Text.ToString(); Items[2] = SourceNode.FirstNode.Text.ToString(); Items[3] = SourceNode.LastNode.Text.ToString(); lstMultiProItems.Items.Clear(); lstMultiProItems.Columns.Add("ID", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Product Name", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Selling Price", 100, HorizontalAlignment.Left); lstMultiProItems.Columns.Add("Conversion", 100, HorizontalAlignment.Left); ListViewItem Item1 = new ListViewItem(Items); lstMultiProItems.Items.Add(Item1); lstMultiProItems.AllowColumnReorder = false; lstMultiProItems.FullRowSelect = true; } }

              U Offline
              U Offline
              Udayaraju
              wrote on last edited by
              #6

              Guys Please Help me out, I have added the event handlers from front end design. but still the event is not fired. Can u please tel me wther the code is correct or not

              1 Reply Last reply
              0
              • D DaveyM69

                Check out the working code below and compare - you should be able to figure it out from this.

                /\* Assumes
                TreeView treeView1. Parent node must have Tag "Parent" and
                exactly two child nodes for drag to work
                ListView listView1 with three columns already added.
                
                Events subscribed
                treeView1.ItemDrag += new ItemDragEventHandler(treeView1\_ItemDrag);
                listView1.DragOver += new DragEventHandler(listView1\_DragOver);
                listView1.DragDrop += new DragEventHandler(listView1\_DragDrop);
                \*/
                void treeView1\_ItemDrag(object sender, ItemDragEventArgs e)
                {
                    TreeNode nodeData = (TreeNode)e.Item;
                    // set as selected
                    treeView1.SelectedNode = nodeData;
                    // check is parent using tag property
                    if ((string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                        // OK
                        treeView1.DoDragDrop(nodeData, DragDropEffects.Copy);
                }
                void listView1\_DragOver(object sender, DragEventArgs e)
                {
                    // default to none
                    e.Effect = DragDropEffects.None;
                    // get tree node
                    TreeNode nodeData = (TreeNode)e.Data.GetData(typeof(TreeNode));
                    // check data in case coming from elsewhere
                    if (nodeData != null && (string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                        // OK so proceed
                        e.Effect = DragDropEffects.Copy;
                
                }
                void listView1\_DragDrop(object sender, DragEventArgs e)
                {
                    // get tree node
                    TreeNode nodeData = (TreeNode)e.Data.GetData(typeof(TreeNode));
                    // check data
                    if (nodeData != null && (string)nodeData.Tag == "Parent" && nodeData.Nodes.Count == 2)
                    {
                        // convert
                        ListViewItem newItem = new ListViewItem(nodeData.Text);
                        foreach (TreeNode childNode in nodeData.Nodes)
                        {
                            ListViewItem.ListViewSubItem newSub = new ListViewItem.ListViewSubItem();
                            newSub.Text = childNode.Text;
                            // add children to parent
                            newItem.SubItems.Add(newSub);
                        }
                        // add parent
                        listView1.Items.Add(newItem);
                    }
                }
                

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is n

                U Offline
                U Offline
                Udayaraju
                wrote on last edited by
                #7

                Thanq very mUch Dave..... these may help me a Lot. I wil go through It and cum with u New errors.. Jus Kidding:thumbsup:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups