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

Grid row reordering with self-referencing hierarchy

7 Answers 101 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Max McKinney
Top achievements
Rank 1
Max McKinney asked on 12 Nov 2011, 01:51 AM
I have a RadGrid that I'm using self-referencing hierarchy and row reordering.  I would like an example of how to trap whether a parent row is being dropped as a child of itself.

For tree view, I use something like:

if(sourceNode.Equals(destNode) || sourceNode.IsAncestorOf(destinationNode))
return;


7 Answers, 1 is accepted

Sort by
0
Max McKinney
Top achievements
Rank 1
answered on 14 Nov 2011, 07:51 PM
does anyone have any idea how to go about doing this?  we're still struggling with finding a solution.
0
Max McKinney
Top achievements
Rank 1
answered on 15 Nov 2011, 11:32 PM
still looking for help please.
0
Marin
Telerik team
answered on 16 Nov 2011, 11:53 AM
Hello Max,

 You can handle the RowDrop event on the server and there you have access to the destination item and the dragged items. Once you have retrieved them you can determine whether one is child of the other by examining their DataKeyNames or specific values of their columns. Here is a sample code based on the hierarchy in the live demo for self-referencing hierarchy:

protected void RadGrid1_RowDrop(object sender, GridDragDropEventArgs e)
        {
            var sourceItemID = e.DraggedItems[0]["EmployeeID"].Text;
 
            var destinationItem = e.DestDataItem.ChildItem.NestedTableViews[0];
 
            if (sourceItemID == destinationItem.ParentItem["ReportsTo"].Text)
            {
                //dragging the item to a child of itslef
            }
        }


Regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Max McKinney
Top achievements
Rank 1
answered on 16 Nov 2011, 07:40 PM
Thanks for the reply, I really appreciate it.  This solution would seem to work as long as the destination is a direct child of the dropped parent.  It seems to me this would not work if you dropped a parent onto a child whose parent was a child of the parent (i.e. nested relationship 2+ levels deep).  Surely there must be some solution that detects if the destination is a child no matter what depth within the tree.  Dropping a parent into a child would have obvious negative affects on the hierarchy so I must find a solution.

Thanks again for all the help.
0
Accepted
Marin
Telerik team
answered on 18 Nov 2011, 10:43 AM
Hello Max,

 Here is what the recursive version of the code will look like. It should work arbitrary level deep.

protected void RadGrid1_RowDrop(object sender, GridDragDropEventArgs e)
        {
            var sourceItemID = e.DraggedItems[0]["EmployeeID"].Text;
 
            var destinationItem = e.DestDataItem;
 
            if (IsChildOrDescendant(sourceItemID, destinationItem))
            {
                //dragging the item to a child of itslef
            }
        }
 
        private bool IsChildOrDescendant(string sourceItemID, GridDataItem destinationItem)
        {
            var parentItem = destinationItem.OwnerTableView.ParentItem;
            if (parentItem==null)
            {
                return false;
            }
            if (sourceItemID == parentItem["EmployeeID"].Text)
            {
                return true;
            }
            else
            {
                //recursive call
                return IsChildOrDescendant(sourceItemID, parentItem);
            }
        }


Best wishes,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Max McKinney
Top achievements
Rank 1
answered on 18 Nov 2011, 06:38 PM
thank you kindly.  this is exactly what I needed.
0
Max McKinney
Top achievements
Rank 1
answered on 18 Nov 2011, 09:38 PM
In case someone else is interested, I made a few small changes that make the example more relevant to self referencing hierarchy:

protected void RadGrid1_RowDrop(object sender, GridDragDropEventArgs e)
        {
            var sourceItemID = e.DraggedItems[0].GetDataKeyValue("ContentID").ToString();
   
            var destinationItem = e.DestDataItem;
   
            if (IsChildOrDescendant(sourceItemID, destinationItem))
            {
                //dragging the item to a child of itslef
            }
        }
   
        private bool IsChildOrDescendant(string sourceItemID, GridDataItem destinationItem)
        {
            if (destinationItem==null)
            {
                return false;
            }
            if (sourceItemID == destinationItem.GetDataKeyValue("ParentContentID").ToString())
            {
                return true;
            }
            else
            {
                //recursive call
                return IsChildOrDescendant(sourceItemID, destinationItem.OwnerTableView.ParentItem);
            }
        }
Tags
Grid
Asked by
Max McKinney
Top achievements
Rank 1
Answers by
Max McKinney
Top achievements
Rank 1
Marin
Telerik team
Share this question
or