I am trying to drag a text item from a Radgridview and drop onto a Node of RadTreeView. I have a custom code that is executed when it is dropped onto the node. My problem is, my treeView has around 200 nodes. I want the treeview to scroll down automatically, when the mouse is moved over ( to the visible last node), with the dragged item. How do I achieve this? Any help will be greatly appreciated.
Thanks,
Ranjitha
3 Answers, 1 is accepted
Hello, Ranjitha,
According to the provided information, it is not clear enough what is the exact custom implementation that you have on your end. However, I believe that you are using the RadGridViewDragDropService that handles the whole drag and drop operation from RadGridView to any other RadControl: https://docs.telerik.com/devtools/winforms/controls/gridview/drag-and-drop/radgridviewdragdropserviceRadTreeView supports auto-scroll on dragging behavior. But it is accomplished by the TreeViewDragDropService. The RadGridViewDragDropService which executes the drag/drop operation in this case is not expected to perform such an action.
The possible solution that I can suggest is to scroll programmatically the RadTreeView control in the RadGridViewDragDropService.PreviewDragOver event:
private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
{
if (e.DragInstance is GridDataRowElement)
{
e.CanDrop = e.HitTarget is RadTreeView ||
e.HitTarget is RadTreeViewElement ||
e.HitTarget is RadTreeNode ||
e.HitTarget is TreeNodeElement;
TreeNodeElement targetNodeElement = e.HitTarget as TreeNodeElement;
if (targetNodeElement!=null)
{
MethodInfo mi = typeof(RadTreeViewElement).GetMethod("AutoScrollOnDragging", BindingFlags.Instance| BindingFlags.NonPublic);
mi.Invoke(targetNodeElement.TreeViewElement,new object[]{targetNodeElement});
}
}
}
You can find attached a complete sample project which result is illustrated in the provided gif file. Please have in mind that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Below is my code:
private void grid_MouseDown(object sender, MouseEventArgs e)
{
RadElement element = grid.ElementTree.GetElementAtPoint(e.Location);
if (element is GridHeaderCellElement || element is ScrollBarThumb || element is GridFilterCellElement) return;
if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
List<string> coll = new List<string>();
GridViewSelectedRowsCollection rows = grid.SelectedRows;
foreach(var row in rows)
{
coll.Add(row.Cells["Name"].Value.ToString());
}
treeview.DoDragDrop(coll, DragDropEffects.Copy);
}
}
private void treeview_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void treeview_DragDrop(object sender, DragEventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
if (e.Data.GetDataPresent(typeof(List<string>)))
{
List<string> values = (List<string>)e.Data.GetData(typeof(List<string>));
if (values == null || values.Count == 0) return;
Point target = treeview.PointToClient(new Point(e.X, e.Y));
RadTreeNode node = treeview.GetNodeAt(target);
//Call my time taking API
}
Cursor.Current = Cursors.Default;
}
I am basically fetching cell values from multiple selected rows in the grid when drag operation is started, and when its dropped onto a tree node, an API is called to do some operation with the string list. No new nodes will be created in the treeview
Hi, Ranjitha,
I can see that you use the OLE drag-and-drop functionality that all WinForms controls support. It is up to you how the custom drag-drop behavior will be accomplished on your end. I believe that the previously mentioned RadGridViewDragDropService can only improve handling the drag-drop operation on your end.
However, the invoked AutoScrollOnDragging RadTreeView's method actually executes the scroll behavior in the tree view. Alternatively, you can manage the TreeViewElement.VScrollBar's value.
Feel free to use this approach which suits your scenario best. Should you have further questions please let me know.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.