Search results

Rows in ASP.NET MVC Gantt control

Row represents a task information from the data source, and it is possible to perform the following actions in Gantt rows.

Row height

It is possible to change the height of the row in Gantt by setting row height in pixels to the RowHeight property. The following code example explains how to change the row height in Gantt at load time.

razor
rowHeight.cs
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").RowHeight(60).TaskFields(ts =>
  ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")).Render()
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Expand/Collapse Row

In Gantt parent tasks are expanded/collapsed by using expand/collapse icons, expand all/collapse all toolbar items and by using public methods. By default all tasks in Gantt was rendered in expanded state but we can change this status in Gantt.

Collapse all tasks at Gantt load

All tasks available in Gantt was rendered in collapsed state by setting CollapseAllParentTasks property as true. The following code example shows how to use this property.

razor
enableCollapseAll.cs
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").CollapseAllParentTasks(true).TaskFields(ts =>
         ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks")
         ).Render()
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Define expand/collapse status of tasks

In Gantt, we can render some tasks in collapsed state and some tasks in expanded state, this can done by defining expand status of the task in data source. This value was mapped to Gantt control by using ExpandState property. The following code example shows how to use this property.

razor
expandState.cs
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").TaskFields(ts => ts.Id("TaskId").Name(
                "TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Child("SubTasks").ExpandState(
                        "isExpand")).Render()
public IActionResult Index()
{
     ViewBag.DataSource = ProjectNewData();
     return View();
}

public static List<GanttDataSource> ProjectNewData()
        {
            List<GanttDataSource> GanttDataSourceCollection = new List<GanttDataSource>();

            GanttDataSource Record1 = new GanttDataSource()
            {
                TaskId = 1,
                TaskName = "Project Initiation",
                StartDate = new DateTime(2019, 04, 02),
                EndDate = new DateTime(2019, 04, 21),
                SubTasks = new List<GanttDataSource>(),
                isExpand = true
            };

            GanttDataSource Child1 = new GanttDataSource()
            {
                TaskId = 2,
                TaskName = "Identify Site location",
                StartDate = new DateTime(2019, 04, 02),
                Progress = 50,
                Duration = 3
            };

            GanttDataSource Child2 = new GanttDataSource()
            {
                TaskId = 3,
                TaskName = "Perform Soil test",
                StartDate = new DateTime(2019, 04, 02),
                Duration = 3,
                Progress = 50,
               
            };

            GanttDataSource Child3 = new GanttDataSource()
            {
                TaskId = 4,
                TaskName = "Soil test approval",
                StartDate = new DateTime(2019, 04, 02),
                Progress = 50,
                Duration = 3,
               
            };
            Record1.SubTasks.Add(Child1);
            Record1.SubTasks.Add(Child2);
            Record1.SubTasks.Add(Child3);

            GanttDataSource Record2 = new GanttDataSource()
            {
                TaskId = 5,
                TaskName = "Project Estimation",
                StartDate = new DateTime(2019, 04, 02),
                Duration = 0,
                Progress = 50,
                SubTasks = new List<GanttDataSource>(),
                isExpand = false

            };
            GanttDataSource Child4 = new GanttDataSource()
            {
                TaskId = 6,
                TaskName = "Develop floor plan for estimation",
                StartDate = new DateTime(2019, 04, 04),
                Duration = 4,
                Progress = 50,
                EndDate = new DateTime(2019, 04, 21),
                SubTasks = new List<GanttDataSource>(),
            };

            GanttDataSource Child5 = new GanttDataSource()
            {
                TaskId = 7,
                TaskName = "List materials",
                StartDate = new DateTime(2019, 04, 04),
                Duration = 4,
                Progress = 50,
                
            };

            Record2.SubTasks.Add(Child4);
            Record2.SubTasks.Add(Child5);

            GanttDataSourceCollection.Add(Record1);
            GanttDataSourceCollection.Add(Record2);

            return GanttDataSourceCollection;
        }

        public class GanttDataSource
        {
            public int TaskId { get; set; }
            public string TaskName { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public int? Duration { get; set; }
            public int Progress { get; set; }
            public bool isExpand { get; set; }
            public List<GanttDataSource> SubTasks { get; set; }
        }

Alt text

Customize expand/collapse action

On expand action Expanding and Expanded event will be triggered with current expanding row’s information. Similarly on collapse action Collapsing and Collapsed event will be triggered. Using this events and it’s arguments we can customize the expand/collapse action. The following code example shows how to prevent the particular row from expand/collapse action using Expanding and Collapsing event.

razor
expandCollapseEvent.cs
@Html.EJS().Gantt("Gantt").DataSource((IEnumerable<object>)ViewBag.DataSource).Height("450px").Collapsing("collapsing").Expanding(
     "expanding").TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration").Progress(
         "Progress").Child("SubTasks")).Render()
		   
    <script>
        function collapsing(args) {
             if (args.data.TaskId == 1) // we can't collapse Task Id 1
                args.cancel = true; 
        }
        function expanding(args) {
            if (args.data.TaskId == 5) // we can't expand Task Id 5
                args.cancel = true;
        }
    </script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Drag and drop

You can dynamically rearrange the rows in the Gantt control by using the AllowRowDragAndDrop property. Using this property, row drag and drop can be enabled or disabled in Gantt. Using this feature, rows can be dropped at above and below as a sibling or child to the existing rows

razor
defaultdrag.cs
@Html.EJS().Gantt("DragAndDrop").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowRowDragAndDrop(true).Height("450px").HighlightWeekends
 (true).TreeColumnIndex(1).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration("Duration"
 ).Progress("Progress").Dependency("Predecessor").Child("SubTasks")).ProjectStartDate("03/24/2019").ProjectEndDate("07/06/2019").Render()
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Multiple row drag and drop

Gantt also supports dragging multiple rows at a time and drop them on any rows above, below, or at child positions. In Gantt, you can enable the multiple drag and drop by setting the SelectionSettings.Type to Multiple and you should enable the AllowRowDragAndDrop property.

razor
multidrag.cs
@Html.EJS().Gantt("DragAndDrop").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowRowDragAndDrop(true).Height("450px").HighlightWeekends(
    true).TreeColumnIndex(1).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate("EndDate").Duration(
      "Duration").Progress("Progress").Dependency("Predecessor").Child("SubTasks")).SelectionSettings(sel =>
		   {
			sel.Type(SelectionType.Multiple);
		}).ProjectStartDate("03/24/2019").ProjectEndDate("07/06/2019").Render()
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Alt text

Drag and drop events

We provide various events to customize the row drag and drop action, the following table explains about the available events and its details.

Event Name Description
RowDragStartHelper Triggers when clicking the drag icon or Gantt row.
RowDragStart Triggers when drag action starts in Gantt.
RowDrag Triggers while dragging the Gantt row.
RowDrop Triggers when a drag row was dropped on the target row.

Customize row drag and drop action

In Gantt, the RowDragStartHelper and RowDrop events are triggered on row drag and drop action. Using this event, you can prevent dragging of particular record, validate the drop position, and cancel the drop action based on the target record and dragged record. The following topics explains about this.

Prevent dragging of particular record

You can prevent drag action of the particular record by setting the cancel property to true, which is available in the RowDragStartHelper event argument based on our requirement. In the following sample, drag action was restricted for first parent record and its child records.

razor
preventdrag.cs
@Html.EJS().Gantt("DragAndDrop").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowRowDragAndDrop(true).Height("450px"
    ).HighlightWeekends(true).TreeColumnIndex(1).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate(
        "EndDate").Duration("Duration").Progress("Progress").Dependency("Predecessor").Child("SubTasks")).RowDragStartHelper(
            "rowDragStartHelper").ProjectStartDate("03/24/2019").ProjectEndDate("07/06/2019").Render()
         
  <script>
    function rowDragStartHelper(args) {
        var record = args.data[0] ? args.data[0] : args.data;
        var taskId = record.ganttProperties.taskId;
        if (taskId <= 4) {
            args.cancel = true;
        }
    }
  </script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Validating drop position

You can prevent drop action based on the drop position and target record, by this, you can prevent dropping particular task on a specific task or specific position. This can be achieved by setting the cancel property to true, which is available in the RowDrop event argument.

In the following sample, we have prevented the drop action based on the position. In this sample, you cannot drop row as child in any of the available rows.

razor
preventdrop.cs
@Html.EJS().Gantt("DragAndDrop").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowRowDragAndDrop(true).Height(
        "450px").HighlightWeekends(true).TreeColumnIndex(1).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate(
            "StartDate").EndDate("EndDate").Duration("Duration").Progress("Progress").Dependency("Predecessor").Child("SubTasks")
            ).RowDrop("rowDrop").ProjectStartDate("03/24/2019").ProjectEndDate("07/06/2019").Render()
  
  <script>
    function rowDrop(args) {
        if (args.dropPosition == "middleSegment") {
            args.cancel = true;
        }
    }
  </script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}

Perform row drag and drop action programmatically

Gantt provides option to perform row drag and drop action programmatically by using the reorderRows method, this method can be used for any external actions like button click. The following arguments are used to specify the positions to drag and drop a row:

  • fromIndexes: Index value of source(dragging) row.
  • toIndex: Value of target index.
  • position: Drop positions such as above, below, or child.

The following code example shows how to drag and drop a row on button click action.

razor
dynamicdrag.cs
@Html.EJS().Button("dynamicDrag").Content("Drop records as child").CssClass("e-primary").Render()
    @Html.EJS().Gantt("DragAndDrop").DataSource((IEnumerable<object>)ViewBag.DataSource).AllowRowDragAndDrop(true).Height(
         "450px").HighlightWeekends(true).TreeColumnIndex(1).TaskFields(ts => ts.Id("TaskId").Name("TaskName").StartDate("StartDate").EndDate
         ("EndDate").Duration("Duration").Progress("Progress").Dependency("Predecessor").Child("SubTasks")).ProjectStartDate(
              "03/24/2019").ProjectEndDate("07/06/2019").Render()
    
    <script>
	  document.getElementById('dynamicDrag').addEventListener('click', function (args) {
         var ganttObj = document.getElementById('DragAndDrop').ej2_instances[0];
         ganttObj.reorderRows([1, 2, 3], 4, 'child');
      });
	</script>
public IActionResult Index()
{
    ViewBag.DataSource = GanttData.ProjectNewData();
    return View();
}