Drag and Drop Treeview Item into listview
-
: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:
-
: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:
-
: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:
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) -
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)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; } }
-
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; } }
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 -
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; } }
-
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