1

我想从树视图拖放到数据网格视图。的代码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>
4

1 回答 1

0

最后,这是解决方案。只需从拖动函数定义树节点并将参数指定到数据网格视图中。

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

            }
于 2012-06-19T10:13:17.827 回答