Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:36:37+00:00 2026-06-06T00:36:37+00:00

I want to drag&drop from treeview to datagrid view. The code for drag is

  • 0

I want to drag&drop from treeview to datagrid view. The code for drag is working fine but the code for dropis not working. 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:

<Product>

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

</Product>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:36:39+00:00Added an answer on June 6, 2026 at 12:36 am

    Finally, here is the solution. Just have to define the treenode from drag function and specify the parameters into the 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;
                    }
    
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement drag&drop possibilities in my application. Here is my source code:
I want to drag data from a ListView and drop it in a TreeView(the
I want to allow users to drag and drop (or select) a file from
I want to do a simple drag-drop using jQuery. I have not done anything
It's not code-related but IDE related. I'm working on a .NET solution with about
Does anyone already implemented Drag & Drop of email messages from Outlook and/or Thunderbird
I would like to implement a drag&drop operation from TextBox to another control. The
I want to handle the drag & drop of hyperlinks in my app. The
I'm struggling with getting drag&drop to work. I want to be able to drag&drop
I want to use a drag to select feature, but rather than select a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.