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

时间:2012-06-19 08:14:52

标签: c# xml winforms datagridview treeview

我想从树视图拖放到数据网格视图。 drag的代码工作正常,但drop的代码无效。请告诉我我在这里做的错误是什么???

我无法为数据集添加新值。

 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:

<Product>

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

</Product>

1 个答案:

答案 0 :(得分:0)

最后,这是解决方案。只需从拖动函数定义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;
                }

            }