DataGridView Drag to the TreeView

Source: Internet
Author: User

There are a lot of times when we have this requirement: there are two grids in a form, and the data table structure in two grids is similar, we want to add data from one grid to another grid. As a general practice, add an import or Export button, select the target line, and trigger the event via a button to increase or decrease the data in both grids.

Hey, but if we can just drag the selected row to another grid with the mouse after the target line is selected, isn't it cool?

Let's take a look at how to implement it.

First, create the sample data.

We still use the previous section: play a DataGridView of the expansion and contraction of the data script, and then use it to create a new two table on the line.

SELECT * into Department_a from Department where Dparentid=1select * to Department_b from Department where dparentid=2

Let's talk about the idea: the effect is that when you select a row in the source grid, hold down the left mouse button, drag the selected row from the source grid and drag it into the target grid. There are four actions that are made:

1. Select a row (one or more lines can be)

2. Drag and Drop lines

3. Remove the selected row from the source grid (you can actually do it without removing it, see the actual demand, in this case, remove the row from the grid)

4. New selected row in target grid.

Let's analyze each of them. I built two grid:sourcegrid sources in the project, Targetgrid is the goal.

1. Select BOC

We must first have a global variable to hold the selected row.

Private Datagridviewselectedrowcollection sourcerowcollection = null;//used to save the selected row

Assigning a value to Sourcerowcollection is, of course, in the Sourcegrid_mousedown event. Here we also need to ensure that the mouse click in the active area to give sourcerowcollection assignment.

Here is useful to a class hittestinfo,nnd, do not know why Microsoft so named, I looked for a long time. This class can get the rowindex and columnindex of the current mouse.

private void Sourcegrid_mousedown (object sender, MouseEventArgs e)        {            //capture the mouse click area information            Datagridview.hittestinfo hittestinfo= this.sourceGrid.HitTest (e.x, e.y);            if (e.x < && hittestinfo.rowindex >-1)            {                if (This.sourceGrid.SelectedRows.Count > 0)                { C8/>sourcerowcollection = this.sourceGrid.SelectedRows;                }            }            else                sourcerowcollection = null;        }

2. Drag and Drop lines

A very important way is to drag and drop a line: DoDragDrop, it has parameters, one is to drag the data, one to achieve the effect. It is important to call the post DoDragDrop to trigger events such as DragOver, DragDrop, and so on for the target control (in this case, Targetgrid). Of course, the premise is that your target control AllowDrop to true. I was because AllowDrop was not set to true, did not trigger the DragOver event, I have been blind for a long time reason.

For DoDragDrop, refer to: http://msdn.microsoft.com/zh-cn/library/ie/system.windows.forms.control.dodragdrop.aspx here is a more detailed introduction, there is also a good example.

We're going to call the DoDragDrop method in Sourcegrid_mousemove:

private void Sourcegrid_mousemove (object sender, MouseEventArgs e)        {            if (E.button = = MouseButtons.Left)            { C3/>if (sourcerowcollection! = null)                {                    DragDropEffects effect = This.sourceGrid.DoDragDrop ( Sourcerowcollection, dragdropeffects.move);                    if (effect = = DragDropEffects.Move)                    {                        //Remove the selected row in Sourcegrid                        foreach (DataGridViewRow row in Sourcerowcollection)                        {                            this.sourceGrid.Rows.Remove (row);                        }                        Reset sourcerowcollection to null                        sourcerowcollection = NULL;}}}        

Note: effect = = DragDropEffects.Move will execute after an event such as DragDrop of the target control is executed.
The Targetgrid_dragover event is triggered when you move to the target form, where we set the value of DragDropEffects.

private void Targetgrid_dragover (object sender, DragEventArgs e)        {            if (!e.data.getdatapresent (typeof ( datagridviewselectedrowcollection))            {                e.effect = DragDropEffects.None;                return;            }            else            {                e.effect = DragDropEffects.Move;  This value is returned to the DoDragDrop method            }        }

When the drag is complete, the DragDrop is triggered and we assign the dragged line to targetgrid here.

View Code

3. Removing the selected rows from the source grid

Removing the selected line is already in the Sourcegrid_mousemove event, i.e.:

if (effect = = DragDropEffects.Move)                    {                        //Remove the selected row in Sourcegrid                        foreach (DataGridViewRow row in Sourcerowcollection)                        {                            this.sourceGrid.Rows.Remove (row);                        }                        Reset sourcerowcollection to null                        sourcerowcollection = null;                    }

4. New row selected in target grid

The code for the new line is also in Targetgrid_dragdrop, as this is triggered when the drag is complete. Here we add and insert the situation, see the above code.

Finally, I only do the interface of the two grid data increase or decrease, and did not save the changes to the database, we are interested can be implemented by themselves.

PS: It seems that two grids in the same form cannot have the same column names, which I am uncomfortable with, and I have to write a different method when I bind the data.

Source code: Datagriddemo, drag-and-drop the form is the Movegridform in the project

DataGridView Drag to the TreeView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.