简体   繁体   English

C#:从树视图拖放到数据集XML文件

[英]C#:Drag&Drop from treeview to dataset XML file

I want to drag&drop from treeview to datagrid view. 我想从treeview拖放到datagrid视图。 The code for drag is working fine but the code for drop is not working. 对于代码drag工作正常,但对于代码drop不工作。 Please tell me what is the mistake am i doing here??? 请告诉我我在这里做什么错误???

I couldn't new values to the dataset. 我无法为数据集添加新值。

 private void DataGridView1OnDragDrop(object sender, DragEventArgs e)
        {
            Point dscreen = new Point(e.X, e.Y);
            Point dclient = dataGridView1.PointToClient(dscreen);
            DataGridView.HitTestInfo hitTest = dataGridView1.HitTest(dclient.X, dclient.Y);

            if (hitTest.ColumnIndex == 0 && hitTest.Type == DataGridViewHitTestType.Cell)
            {
                e.Effect = DragDropEffects.Move;
                var ds = (DataSet) dataGridView1.DataSource;
                dataGridView1.Rows.Insert(hitTest.RowIndex, "test", "test", "test", "test");

            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

  private void getDataGridFromXml()
        {
            try
            {
                XmlReader xmlFile;
                xmlFile = XmlReader.Create(@"C:\Depth.xml", new XmlReaderSettings());
                DataSet ds = new DataSet();
                ds.ReadXml(xmlFile);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

XMl: XMl:

<Product>

<Apple>
<Type>Best</Type>
<Trace>Spain</Trace>
<Quantity>1000</Quantity>
<Description>Notihng</Description>
</Apple>

</Product>

Finally, here is the solution. 最后,这是解决方案。 Just have to define the treenode from drag function and specify the parameters into the datagridview. 只需从拖动功能定义treenode并将参数指定到datagridview中即可。

  private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
            {
                // Move the dragged node when the left mouse button is used.
                var node = e.Item as TreeNode;

                if(node.Parent == null)
                    return;

                var root = FindTraceRootNode(node);
                var i = new string[] { root.Nodes[0].Nodes[0].Text, 
                    root.Nodes[1].Nodes[0].Text, 
                    root.Nodes[2].Nodes[0].Text, 
                    root.Nodes[3].Nodes[0].Text };

                if (e.Button == MouseButtons.Left)
                {
                    DoDragDrop(i, DragDropEffects.Move);
                }

                // Copy the dragged node when the right mouse button is used.
                else if (e.Button == MouseButtons.Right)
                {
                    DoDragDrop(i, DragDropEffects.Copy);
                }
            }

            private TreeNode FindTraceRootNode(TreeNode node)
            {
                while (node.Parent != treeView1.Nodes[0])
                {
                    node = node.Parent;
                }
                return node;
            }

            // Set the target drop effect to the effect 
            // specified in the ItemDrag event handler.
            private void treeView1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = e.AllowedEffect;
            }

            // Select the node under the mouse pointer to indicate the 
            // expected drop location.
            private void TreeView1OnDragOver(object sender, DragEventArgs e)
            {
                // Retrieve the client coordinates of the mouse position.
                Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));

                // Select the node at the mouse position.
                treeView1.SelectedNode = treeView1.GetNodeAt(targetPoint);
            }


            private void DataGridView1OnDragOver(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Move;
            }

            private void DataGridView1OnDragDrop(object sender, DragEventArgs e)
            {
                Point dscreen = new Point(e.X, e.Y);
                Point dclient = dataGridView1.PointToClient(dscreen);
                DataGridView.HitTestInfo hitTest = dataGridView1.HitTest(dclient.X, dclient.Y);

                if (hitTest.ColumnIndex == 0 && hitTest.Type == DataGridViewHitTestType.Cell)
                {
                    e.Effect = DragDropEffects.Move;
                    //dataGridView1.Rows.Insert(hitTest.RowIndex, "hitTest", "hitTest", "hitTest", "hitTest");
                    var data = (object[]) e.Data.GetData(typeof(string[]));
                    dataGridView1.Rows.Insert(hitTest.RowIndex, data);
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM