This is a migrated thread and some comments may be shown as answers.

Drag and drop to/from multiple grids

9 Answers 89 Views
GridView
This is a migrated thread and some comments may be shown as answers.
steve
Top achievements
Rank 1
steve asked on 16 Dec 2009, 12:45 AM
Using the the example in this link I am trying to figure the best way to drop a row only on the specific grid you want to add it to.

For example, I have 4 grids in a docking panel.  lets call them grids1 - grid4.  grid1 and grid2 has values I want to share with grid2. grids 3 and grid4 do not except any drop.  However, what is happening is that when I drag from grid1 to grid4 it addes the value to grid2.  How can I tell if the grid I am hovering over is the grid that I want to add the value to.

Thanks
Stephen

9 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 17 Dec 2009, 05:02 PM
Hi steve,

I am afraid that I have not enough information about how exactly you have tried to implement Drag And Drop scenario. Determining on which grid you are dropping the dragged row is specific for the chosen implementation. If you are using MouseUp event to finnish dropping, you can check which RadGridView contains the cursor. For example:
private void rgvDrag_MouseUp(object sender, MouseEventArgs e)
{
    var screenPoint = rgvDrag.PointToScreen(e.Location);
    var point = PointToClient(screenPoint );
              
    if (radGridView2.Bounds.Contains(point))
    {
       //....
    }
    else if (radGridView3.Bounds.Contains(point))
    {
       //....
    }
}

If this does not match your scenario, please open a new support ticket and send us a small example project that demonstrates your case. This will help us to investigate the problem further and provide more accurate assistance.

Kind regards,

Martin Vasilev
the Telerik team

 


Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
steve
Top achievements
Rank 1
answered on 17 Dec 2009, 05:09 PM
I think that is the solution I am looking for.  I will try it and get back with you.  Thank you.
0
steve
Top achievements
Rank 1
answered on 23 Dec 2009, 10:15 PM
Well it does work for one grid but not the other.  I am assuming that line (radGridView2.Bounds.Contains(point)) says, if the mouse points to grid 2 then execute code.  The problem I have is that the bounds of both grids are x=0 y=0 because they are in a split container. Is there a another way to get the postion of the grid in the parent container?
0
Jack
Telerik team
answered on 28 Dec 2009, 08:56 AM
Hi steve,

When using MouseUp event - as in the sample - you will get coordinates relative to the grid that started the drag/drop operation. First you have to convert these coordinates to screen coordinates just like in the sample. You should convert also the location of your grids in screen coordinates. Here is a sample:

Rectangle bounds = new Rectangle(this.radGridView2.PointToScreen(new Point(0, 0)), this.radGridView2.Size);

Using screen coordinates it will be easy to determine which grid contains the point:

void rgvDrag_MouseUp(object sender, MouseEventArgs e)
{
    Point screenPoint = rgvDrag.PointToScreen(e.Location);
 
    foreach (RadGridView grid in myGrids)
    {
        Point location = grid.PointToScreen(new Point(0, 0));
        Rectangle bounds = new Rectangle(location, grid.Size);
        if (bounds.Contains(screenPoint))
        {
            DoDragDropOperation();
            return;
        }
    }
}

If you need further assistance, just ask.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
steve
Top achievements
Rank 1
answered on 28 Dec 2009, 05:31 PM
Ok everything makes sense except for foreach (RadGridView grid in myGrids)

I tried using this.Controls.OfType<MyGrid> (MyGrid is a inherited radgridview I use for the grids) but it keeps coming back null.  What to you interrate though to get all of the grids?
0
Robert
Top achievements
Rank 1
answered on 28 Dec 2009, 10:08 PM
Hi Steve,

Since you are using a SplitContainer control, you will need to retrieve the RadGridViews from it's SplitPanels rather than the local form.

            List<MyCustomGrid> myGrids = new List<MyCustomGrid>(); 
            myGrids.AddRange(splitPanel1.Controls.OfType<MyCustomGrid>()); 
            myGrids.AddRange(splitPanel2.Controls.OfType<MyCustomGrid>()); 
 
            foreach (MyCustomGrid gv in myGrids) 
            { 
                // do stuff 
            } 

I hope this helps.

- Robert
0
steve
Top achievements
Rank 1
answered on 29 Dec 2009, 07:45 PM

Yes, Robert that was it.  Thanks.  However, after the first drag and drop I either get a null refernce for the outlineForm (used to create image to the dragged item) or the mouse up pulls the first line of the grid instead of the one I am selecting.  

In the mouse up I clear the outlineform (= null) to get ride of the bitmat.  When go to drag a row from another grid, goes back into the movemouse method and the app thinks I am dragging when I am not.  I cannot figure out where to tell the app I am done dragging and ready to start a new drag.
any suggestions?

  private void GridViewMouseMove(MyCustomGrid fromGrid, MouseEventArgs e)  
        {  
            if (e.Button == MouseButtons.Left)  
            {  
                if (!dragging && IsRealDrag(e.Location, downPt))  
                {  
                    RadElement element = fromGrid.ElementTree.GetElementAtPoint(e.Location);  
                    GridDataCellElement cell = element as GridDataCellElement;  
                    if (cell != null && cell.RowElement is GridDataRowElement)  
                    {  
                        dragging = true;  
                        fromGrid.Capture = true;  
                        fromGrid.Cursor = Cursors.Hand;  
 
                        Bitmap itemBitmap = GetBitmap(fromGrid);  
                        outlineForm = TelerikHelper.CreateOutlineForm(itemBitmap);  
                        outlineForm.MinimumSize = itemBitmap.Size;  
                        outlineForm.Size = itemBitmap.Size;  
                        Point mousePt = Cursor.Position;  
                        outlineForm.Location = new Point(mousePt.X + 5, mousePt.Y + 5);  
                        outlineForm.TopMost = true;  
                        outlineForm.Show();  
                    }  
                }  
                else if (dragging)  
                {  
                    Point mousePt = Control.MousePosition;  
                    outlineForm.Location = new Point(mousePt.X + 5, mousePt.Y + 5);  
                }  
            }  
        }  
        private void GridViewMouseUp(MyCustomGrid fromGrid, MouseEventArgs e)  
        {  
            if (dragging)  
            {  
                if (outlineForm != null && downPt.Equals(e.Location))  
                {  
                    outlineForm.Close();  
                    outlineForm.Dispose();  
                    outlineForm = null;  
                }  
 
                Point screenPoint = fromGrid.PointToScreen(e.Location);  
                List<MyCustomGrid> myGrids = new List<MyCustomGrid>();  
                myGrids.AddRange(splitPanel3.Controls.OfType<MyCustomGrid>());  
                myGrids.AddRange(splitPanel4.Controls.OfType<MyCustomGrid>());  
 
                foreach (MyCustomGrid grid in myGrids)  
                {  
                    Point location = grid.PointToScreen(new Point(0, 0));  
                    Rectangle bounds = new Rectangle(location, grid.Size);  
                    if (bounds.Contains(screenPoint))  
                    {  
                        foreach (GridViewDataRowInfo row in fromGrid.RowsToDragDrop)  
                        {  
                            ToRadGridView.Rows.Add(row.Cells[0].Value, row.Cells[1].Value, row.Cells[2].Value);  
                            fromGrid.Rows.Remove(row);  
                        }  
                        return;  
                    }  
                }  
                fromGrid.Cursor = Cursors.Arrow;  
                fromGrid.Capture = false;  
                dragging = false;  
            }  
        } 

0
Jack
Telerik team
answered on 30 Dec 2009, 11:18 AM
Hello steve,

I checked your code. It differs a bit from the code in this forum thread. In your GridViewMouseUp method there is a return which skips the code at the end of the method. This code is important because it resets the dragging operation by setting the dragging variable to false. You should replace this return with a break statement. Should you have any further questions, please write me back.

All the best,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
steve
Top achievements
Rank 1
answered on 30 Dec 2009, 06:56 PM
Ahhhh, yeah not sure what happened there.  But it works perfect now thank you!
Tags
GridView
Asked by
steve
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
steve
Top achievements
Rank 1
Jack
Telerik team
Robert
Top achievements
Rank 1
Share this question
or