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
Thank you for writing us.
- 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; } } - 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.
Best wishes,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
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; |
} |
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
Kind regards
Ralph
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
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.
Dess
Telerik
Hello Dess
Really apreciatted, I will try and let you know. Sorry for the late answer but I was on annual leave.
Thank you
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;
}
}
} ​
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
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