Drag and Drop in ASP.NET CORE Tree Grid Component

21 Dec 202313 minutes to read

The TreeGrid rows can be reordered, dropped to another TreeGrid or custom control by enabling the allowRowDragAndDrop to true.

Drag and drop within Tree Grid

The TreeGrid row drag and drop allows you to drag and drop TreeGrid rows on the same TreeGrid using drag icon. To enable row drag and drop, set the allowRowDragAndDrop to true. It provides the way to drop the row above, below or child to the target row with respective to the target row position.

<ejs-treegrid id="TreeGrid" allowRowDragAndDrop="true" allowSelection="true" dataSource="ViewBag.datasource" height="275" childMapping="Children" treeColumnIndex="1">
     <e-treegrid-selectionsettings type="Multiple"></e-treegrid-selectionsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" 
                format="yMd" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

NOTE

  • Selection feature must be enabled for row drag and drop.

    * For multiple row selection, the type property must be set to multiple.

    * The isPrimaryKey property is necessary to perform row drag and drop operation.

Drag and drop to another Tree Grid

To drag and drop between two TreeGrid, enable the allowRowDragAndDrop property and specify the target TreeGrid ID in targetID property of rowDropSettings.

<ejs-treegrid id="TreeGrid" allowRowDragAndDrop="true" allowSelection="true" dataSource="ViewBag.datasource" height="275" childMapping="Children" treeColumnIndex="1">
     <e-treegrid-selectionsettings type="Multiple"></e-treegrid-selectionsettings>
    <e-treegrid-rowdropsettings targetID="DestGrid"></e-treegrid-rowdropsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" 
                format="yMd" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<ejs-treegrid id="DestGrid" allowRowDragAndDrop="true" allowSelection="true" height="275" childMapping="Children" treeColumnIndex="1">
    <e-treegrid-selectionsettings type="Multiple"></e-treegrid-selectionsettings>
    <e-treegrid-rowdropsettings targetID="DestGrid"></e-treegrid-rowdropsettings>
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" 
                format="yMd" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Drag and drop events

The following events are triggered while drag and drop the treegrid rows.

RowDragStartHelper - Triggers when click the drag icon or treegrid row and this event is used to customize the drag element based on user criteria.

RowDragStart -Triggers when starts to drag the treegrid row.

RowDrag - Triggers while dragging the treegrid row.

RowDrop - Triggers when a drag element is dropped on the target element.

Prevent reordering a row as child to another row

You can prevent the default behavior of dropping rows as children to the target by setting the cancel property to true in rowDrop event argument. You can also change the drop position after cancelling using reorderRows method.

In the below example drop action is cancelled and dropped above to target row.

@{
    var data = TreeGridItems.GetTreeData();
}
<ejs-treegrid id="TreeGrid" allowRowDragAndDrop="true" rowDrop="rowDrop" dataSource="@data" height="275" treeColumnIndex="1" childMapping="Children">
    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="90"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="180"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText=" Start Date" textAlign="Right" 
                format="yMd" width="90"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="80"></e-treegrid-column>
    </e-treegrid-columns>
</ejs-treegrid>

<script>
    function rowDrop(args) {
        if (args.dropPosition == 'middleSegment') {
          var treeGridObj = document.getElementById('TreeGrid').ej2_instances[0];    
          args.cancel = true;
          treeGridObj.reorderRows([args.fromIndex], args.dropIndex, 'above');
        }
    }
</script>
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(TreeGridItems.GetTreeData());
    }
}
public class TreeGridItems
{
    public TreeGridItems() { }
    public int TaskId { get; set; }
    public string TaskName { get; set; }
    public DateTime StartDate { get; set; }
    public int Duration { get; set; }
    public List<TreeGridItems> Children { get; set; }

    public static List<TreeGridItems> GetTreeData()
    {
        List<TreeGridItems> BusinessObjectCollection = new List<TreeGridItems>();

        TreeGridItems Record1 = null;

        Record1 = new TreeGridItems()
        {
            TaskId = 1,
            TaskName = "Planning",
            StartDate = new DateTime(2016, 06, 07),
            Duration = 5,
            Children = new List<TreeGridItems>(),
        };
        TreeGridItems Child1 = new TreeGridItems()
        {
            TaskId = 2,
            TaskName = "Plan timeline",
            StartDate = new DateTime(2016, 06, 07),
            Duration = 5
        };

        TreeGridItems Child2 = new TreeGridItems()
        {
            TaskId = 3,
            TaskName = "Plan budget",
            StartDate = new DateTime(2016, 06, 07),
            Duration = 5
        };
        TreeGridItems Child3 = new TreeGridItems()
        {
            TaskId = 4,
            TaskName = "Allocate resources",
            StartDate = new DateTime(2016, 06, 07),
            Duration = 5
        };
        Record1.Children.Add(Child1);
        Record1.Children.Add(Child2);
        Record1.Children.Add(Child3);
        TreeGridItems Record2 = new TreeGridItems()
        {
            TaskId = 6,
            TaskName = "Design",
            StartDate = new DateTime(2021, 08, 25),
            Duration = 3,
            Children = new List<TreeGridItems>()
        };
        TreeGridItems Child5 = new TreeGridItems()
        {
            TaskId = 7,
            TaskName = "Software Specification",
            StartDate = new DateTime(2021, 08, 25),
            Duration = 3
        };

        TreeGridItems Child6 = new TreeGridItems()
        {
            TaskId = 8,
            TaskName = "Develop prototype",
            StartDate = new DateTime(2021, 08, 25),
            Duration = 3
        };
        TreeGridItems Child7 = new TreeGridItems()
        {
            TaskId = 9,
            TaskName = "Get approval from customer",
            StartDate = new DateTime(2024, 06, 27),
            Duration = 2
        };
        Record2.Children.Add(Child5);
        Record2.Children.Add(Child6);
        Record2.Children.Add(Child7);
        BusinessObjectCollection.Add(Record1);
        BusinessObjectCollection.Add(Record2);
        return BusinessObjectCollection;
    }
}

NOTE

You can refer to our ASP.NET Core Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET Core Tree Grid example ASP.NET Core Tree Grid example to knows how to present and manipulate data.