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

DragDrop event in GridView

10 Answers 366 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ralph
Top achievements
Rank 2
Ralph asked on 29 Jan 2008, 03:31 PM
Hi

1. I need to be able to tell from the DragEventArgs arguments in the DragDrop event of the GridView, which row the user is "dropping" on.

I tried:

dgvPlaylistRegionMedia.GetChildAtPoint(new Point(e.X, e.Y));

but it returns null the whole time.

2. Also, it would be nice to give some sort of indication to the user where he would be "dropping", for e.g. a line between row 2 and row 3 if he's "hovering" between those 2 rows, or even a line after the last row to indicate that he would be adding it after the last row if he's "hovering" past the last row.  Any ideas?

Kind regards

Ralph

10 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 30 Jan 2008, 09:51 AM
Hello Ralph,

Thank you for writing us.

  1. You can find the row at specific point using the GetElementAtPoint method of RadGridView. Consider the code below:

    void radGridView1_DragDrop(object sender, DragEventArgs e) 
        RadElement element = this.radGridView1.GetElementAtPoint(new Point(e.X, e.Y)); 
        GridRowElement row = null
     
        while(element != null
        { 
            if (element is GridRowElement) 
            { 
                row = element; 
                break
            } 
            element = element.Parent; 
        } 
  2. Currently RadGridView can not provide such information. We will consider implementing a GetHitTest method that will return hit test information in one of our future versions.
Don't hesitate to contact us if you have other questions.

Best wishes,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ralph
Top achievements
Rank 2
answered on 30 Jan 2008, 01:21 PM
Hi Jack

I tried what you've suggested but the GetElementAtPoint still only returns null, here's my code:

      RadElement element = dgvPlaylistRegionMedia.GetElementAtPoint(new Point(e.X, e.Y)); 
      GridRowElement row = null
 
      while (element != null
      { 
        if (element is GridRowElement) 
        { 
          row = (GridRowElement)element; 
          break
        } 
 
        element = element.Parent; 
      } 




0
Jack
Telerik team
answered on 31 Jan 2008, 06:19 PM
Hello Ralph,

Please, confirm that the coordinates given to the GetElementAtPoint method are in control coordinates. If they are in screen coordinates you can use the PointToClient method of the RadGridView in order to convert them. If you continue to experience this issue, please send us a sample application in a formal ticket so that we can review it. Thanks in advance.

We will be glad to help you further.

 
Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Ralph
Top achievements
Rank 2
answered on 01 Feb 2008, 08:18 AM
Thanks Jack, the PointToClient did the trick!

Kind regards

Ralph
0
Salva
Top achievements
Rank 2
answered on 22 Jul 2015, 02:57 PM

Hi

 

is there any way of know which is the column related to this point, i'm implementing a drag&drop for cells and i need to know the column of the destination.

Thank you

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Jul 2015, 08:27 AM
Hello Salva,

Thank you for writing.

RadGridView handles the whole drag and operation by using RadGridViewDragDropService. The PreviewDragOver event allows you to control on what targets the element being dragged can be dropped on. The PreviewDragDrop event allows you to get a handle on all the aspects of the drag and drop operation, the source element, the destination (target) element, as well as the element being dragged. This is where we will initiate the actual physical move of the element(s) from one location to the other. You can refer to the GridView >> Drag and Drop help article which demonstrates a sample approach.

The provided information is not enough for me to determine what is the particular drag and drop implementation on your end. However, I suppose that you drag over a GridDataCellElement. You can access the target column via the ColumnInfo property. If it is not applicable for your case, would it possible to provide a complete code snippet of the drag and drop implementation on your end? Alternatively, you can open a support ticket with a sample project. Thus, we would be able to investigate the precise case and assist you further. Thank you in advance.

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Dess
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Salva
Top achievements
Rank 2
answered on 17 Aug 2015, 07:37 AM

Hello Dess

 Really apreciatted, I will try and let you know. Sorry for the late answer but I was on annual leave.

Thank you

0
Salva
Top achievements
Rank 2
answered on 17 Aug 2015, 11:36 AM

Hi Dess

 I have tried several things but i cannot get the drop cell.

My needs are move from one cell to another an integer adding the new quantity to the existing one and changing the dragged to 0.

Example columns A and B of row 1    A has 5 and B has 10, then you drag the B column from row 1 to the A column of row 1 and A will be 15 and B will be 0.

Here is my class, but when i try to get the gridCellElement or the GridDataCellElement i cannot, there is only a stackLayoutElement at the e.DropLocation

 Can you help me? i'm turning mad, thank you

 

class DragAndDropRadGrid : RadGridView
    {
        private static GridDataCellElement dragDestinationCell;
        public DragAndDropRadGrid()
        {
            

            //handle drag and drop events for the grid through the DragDrop service
            RadDragDropService svc =
                    this.GridViewElement.GetService<RadDragDropService>();
            svc.PreviewDragStart += svc_PreviewDragStart;
            svc.PreviewDragDrop += svc_PreviewDragDrop;
            svc.PreviewDragOver += svc_PreviewDragOver;

            //register the custom row selection behavior
            var gridBehavior = this.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new RowSelectionGridBehavior());

        }
        public class RowSelectionGridBehavior :   GridDataRowBehavior
        {
            protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                GridDataCellElement cell = this.GetCellAtPoint(e.Location) as GridDataCellElement;
                if (cell != null)
                {
                  
                    RadGridViewDragDropService svc = this.GridViewElement.GetService<RadGridViewDragDropService>();
                    svc.AllowAutoScrollColumnsWhileDragging = true;
                    svc.AllowAutoScrollRowsWhileDragging = false;
                    svc.Start(cell);
                }
                return base.OnMouseDownLeft(e);
            }
        }
        //required to initiate drag and drop when grid is in bound mode
        private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
        {
            e.CanStart = true;
        }
        private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
        {
            if (e.DragInstance is GridDataCellElement)
            {
                //e.CanDrop = true;
                e.CanDrop = e.HitTarget is GridDataCellElement||
                    e.HitTarget is GridDataRowElement;// ||
                
            }
            
        }
        //gather drag/source grid and target/destination information and initiate the move of selected rows
        private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
        {
            var cellDrag = e.DragInstance as GridDataCellElement;
            GridCellElement dest = e.HitTarget as GridCellElement;

            if (cellDrag == null)
            {
                return;
            }
            e.Handled = true;

            var dropTarget = e.HitTarget as RadItem;
          
            var targetGrid = dropTarget.ElementTree.Control as RadGridView;
            
            GridCellElement cellDrop = this.ElementTree.GetElementAtPoint(e.DropLocation) as GridCellElement; 
            if (targetGrid == null)
            {
                return;
            }

            var dragGrid = cellDrag.ElementTree.Control as RadGridView;
            if (targetGrid == dragGrid)
            {
                
                e.Handled = true;
          
                cellDrop.Value = (int)cellDrag.Value +(int) cellDrop.Value;
                cellDrag.Value=0;
            }


          
        }
    }      â€‹

 

0
Salva
Top achievements
Rank 2
answered on 18 Aug 2015, 11:42 AM

Hi Dess

Finally i have done, but for some reason when i pass directly the e.droplocation to the getElementAtPoint I have issues, fixed converting to screen (only need to convert the Y) and passing the old X coordinate from e.droplocation plus the Y coordinate from the pointtoscreen(e.droplocation)

this is the code that works and do the job

 

class DragAndDropRadGrid : RadGridView
    {
        private static GridDataCellElement dragDestinationCell;
        public DragAndDropRadGrid()
        {
            

            //handle drag and drop events for the grid through the DragDrop service
            RadDragDropService svc =
                    this.GridViewElement.GetService<RadDragDropService>();
            svc.PreviewDragStart += svc_PreviewDragStart;
            svc.PreviewDragDrop += svc_PreviewDragDrop;
            svc.PreviewDragOver += svc_PreviewDragOver;

            //register the custom row selection behavior
            var gridBehavior = this.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new RowSelectionGridBehavior());

        }
        public class RowSelectionGridBehavior :   GridDataRowBehavior
        {
            protected override bool OnMouseDownLeft(MouseEventArgs e)
            {
                GridDataCellElement cell = this.GetCellAtPoint(e.Location) as GridDataCellElement;
                if (cell != null)
                {
                  
                    RadGridViewDragDropService svc = this.GridViewElement.GetService<RadGridViewDragDropService>();
                    svc.AllowAutoScrollColumnsWhileDragging = true;
                    svc.AllowAutoScrollRowsWhileDragging = true;
                    svc.Start(cell);
                }
                return base.OnMouseDownLeft(e);
            }
        }
        //required to initiate drag and drop when grid is in bound mode
        private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
        {
            e.CanStart = true;
        }
        private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
        {
            if (e.DragInstance is GridDataCellElement)
            {
                //e.CanDrop = true;
                e.CanDrop = e.HitTarget is GridDataCellElement||
                    e.HitTarget is GridDataRowElement;// ||
                
            }
            
        }
        //gather drag/source grid and target/destination information and initiate the move of selected rows
        private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
        {
            var cellDrag = e.DragInstance as GridDataCellElement;
            var dest = e.HitTarget as GridDataRowElement;
           
            if (cellDrag == null)
            {
                return;
            }           

            var dropTarget = e.HitTarget as RadItem;
          
            var targetGrid = dropTarget.ElementTree.Control as RadGridView;

            Point coord = this.PointToScreen(e.DropLocation);
           //coord has the wrong X coordinate, we need to ammend with the original one
            coord.X = e.DropLocation.X;
            GridDataCellElement cellDrop = this.ElementTree.GetElementAtPoint(coord) as GridDataCellElement;

            string cellColumnDrop = cellDrop.Data.HeaderText;
            MessageBox.Show(cellColumnDrop);

           GridViewCellInfo  Drop = dest.Data.Cells[cellColumnDrop];
           
            if (cellDrop != null)
            {
                e.Handled = true;
                if (targetGrid == null)
                {
                    return;
                }
                var dragGrid = cellDrag.ElementTree.Control as RadGridView;
                if (targetGrid == dragGrid)
                {

                    e.Handled = true;
                    try
                    {                       
                        Drop.Value = (int)cellDrag.Value + (int)Drop.Value;
                        cellDrag.Value = 0;                       
                    }
                    catch
                    {
                    }
                }

            }
          
        }
    }     â€‹

 

Thank you for your help, now i will clean it a bit and add the restrictions

0
Hristo
Telerik team
answered on 19 Aug 2015, 11:46 AM
Hi Salva,

I am glad that you have found a solution which fits your local setup. 

In case you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Ralph
Top achievements
Rank 2
Answers by
Jack
Telerik team
Ralph
Top achievements
Rank 2
Salva
Top achievements
Rank 2
Dess | Tech Support Engineer, Principal
Telerik team
Hristo
Telerik team
Share this question
or