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

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

5 Answers 96 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 27 Nov 2015, 01:59 PM

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.

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Nov 2015, 03:38 PM
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
0
Simon
Top achievements
Rank 1
answered on 27 Nov 2015, 04:01 PM

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.

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Nov 2015, 01:16 PM
Hello Simon,

Thank you for writing back.

In the 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
0
Scott
Top achievements
Rank 1
answered on 09 Feb 2017, 07:04 PM

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

0
Hristo
Telerik team
answered on 13 Feb 2017, 04:24 PM
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.
Tags
GridView
Asked by
Simon
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Simon
Top achievements
Rank 1
Scott
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or