Drag and drop - Identifying the row AND column of the target

6 posts, 1 answers
  1. Simon
    Simon avatar
    7 posts
    Member since:
    Apr 2014

    Posted 27 Nov 2015 Link to this post

    Hi,

    All the examples I have seen are all about identifying the row of a target GridView where an item has been dropped onto.

    But I need to identify the column as well. Is there a way to do this given only the e.HitTarget from the RadDropEventArgs i.e. in

     

    void dragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)

     

    Thanks for any help.

  2. Dess
    Admin
    Dess avatar
    2101 posts

    Posted 27 Nov 2015 Link to this post

    Hello Simon,

    Thank you for writing.

    I have prepared a sample code snippet demonstrating how to access the target cell element and get the associated column with the cell: 
    public Form1()
    {
        InitializeComponent();
        RadDragDropService svc = this.radGridView1.GridViewElement.GetService<RadDragDropService>();
        svc.PreviewDragStart += svc_PreviewDragStart;
        svc.PreviewDragDrop += svc_PreviewDragDrop;
        svc.PreviewDragOver += svc_PreviewDragOver;
        svc.PreviewDropTarget += svc_PreviewDropTarget;
     
        //register the custom row selection behavior
        var gridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
        gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
        gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new RowSelectionGridBehavior());
    }
     
    //initiates drag and drop service for clicked rows
    public class RowSelectionGridBehavior : GridDataRowBehavior
    {
        protected override bool OnMouseDownLeft(MouseEventArgs e)
        {
            GridDataRowElement row = this.GetRowAtPoint(e.Location) as GridDataRowElement;
            if (row != null)
            {
                RadGridViewDragDropService svc = this.GridViewElement.GetService<RadGridViewDragDropService>();
                svc.AllowAutoScrollColumnsWhileDragging = false;
                svc.AllowAutoScrollRowsWhileDragging = false;
                svc.Start(row);
            }
            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 GridDataRowElement)
        {
            e.CanDrop = e.HitTarget is GridDataRowElement ||
                        e.HitTarget is GridTableElement ||
                        e.HitTarget is GridSummaryRowElement;
        }
    }
     
    GridDataCellElement targetCellElement = null;
     
    private void svc_PreviewDropTarget(object sender, PreviewDropTargetEventArgs e)
    {
        if (e.HitTarget is GridDataCellElement)
        {
            targetCellElement = e.HitTarget as GridDataCellElement;
        }
    }
     
    //gather drag/source grid and target/destination information and initiate the move of selected rows
    private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
    {
        var rowElement = e.DragInstance as GridDataRowElement;
         
        if (rowElement == null)
        {
            return;
        }
        e.Handled = true;
     
        GridDataRowElement targetRowElement = e.HitTarget as GridDataRowElement;
        if (targetCellElement != null)
        {
            Console.WriteLine(targetCellElement.ColumnInfo.Name);
        }
    }

    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
  3. Simon
    Simon avatar
    7 posts
    Member since:
    Apr 2014

    Posted 27 Nov 2015 in reply to Dess Link to this post

    So this uses PreviewDropTarget which has access to the column?

    Great.

    I had found a horrible way round it by using the point.X info in e.HitTarget within svc_PreviewDragDrop and...

    counting up all the col widths until the sum was > point.X to identify the column idx. Although it seems to work, I think I'll try your method....

    Presumably I can reset targetCellElement to NULL after this line:

      Console.WriteLine(targetCellElement.ColumnInfo.Name);  

     

    Many thanks.

  4. Answer
    Dess
    Admin
    Dess avatar
    2101 posts

    Posted 30 Nov 2015 Link to this post

    Hello Simon,

    Thank you for writing back.

    In the PreviewDropTarget you have access to the target cell, from which you can access the column. As to your approach, note that it may not work always if not all columns are visible in the grid and you have to scroll. However, feel free to use this approach which suits your requirement best.

    I hope this information helps. If you have any additional questions, please let me know.

    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
  5. Scott
    Scott avatar
    30 posts
    Member since:
    Dec 2015

    Posted 09 Feb Link to this post

    In a gridview using a bound datasource with grouped rows....

    When dragging and dropping an entire row, and selecting to drop in between 2 rows, sometimes the drop target is the row before my drop line, sometimes my droptarget is after my drop line.

    The switch seems like it might be related to attempting to drop before the first, or after the last, row within a group.  In neither of the aforementioned 2 scenarios does the droptarget return a null; which I would have expected it to do so for one of those scenarios.

    In the end...  I need to write an ordinal value to the drag-dropped row based upon the 2 rows on either side of the drop point, then re-assign ordinals for the following rows within the group.

    Thank you.

    -Scott

  6. Hristo
    Admin
    Hristo avatar
    1018 posts

    Posted 13 Feb Link to this post

    Hello Scott,

    Thank you for writing.

    When the grid is data bound, the drag-and-drop behavior needs be implemented according to data bound objects. In order to investigate your actual scenario please open up a support ticket and attach your project.

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

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top