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

4 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
    1609 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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.

  5. Answer
    Dess
    Admin
    Dess avatar
    1609 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
Back to Top