How to remove Relations in RadGanttView

1 Answer 26 Views
GanttView TimeLine
Leire
Top achievements
Rank 1
Iron
Iron
Leire asked on 09 May 2022, 09:23 AM

Hi,

Is there any way I can select (clicking on) relationships in a RadGanttView so I can remove the relation between two tasks? (whether it's with right click -> delete or with a RadRibbonButton)

1 Answer, 1 is accepted

Sort by
1
Stenly
Telerik team
answered on 12 May 2022, 08:51 AM

Hello Leire,

To achieve the required result, the Dependencies collection of the GanttTask instance, which has a dependency on another GanttTask instance, would have to be modified.

The following code snippet shows a sample implementation of the abovementioned logic, that executes when the PreviewMouseRightButtonDown event of that RadGanttView control is fired:

private void ganttView2_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource is Path)
    {
        var path = e.OriginalSource as Path;

        var parentRelationContainer = path.ParentOfType<RelationContainer>();

        if (parentRelationContainer != null)
        {
            MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(
            "Are you sure you want to delete this relation?",
            "Delete Confirmation",
            System.Windows.MessageBoxButton.YesNo);

            if (messageBoxResult == MessageBoxResult.Yes)
            {
                var dataItem = (RelationInfo)parentRelationContainer.DataItem;

                var taskFrom = dataItem.TaskFrom as GanttTask;
                var taskTo = dataItem.TaskTo as GanttTask;

                var dependency = taskTo.Dependencies.FirstOrDefault(x => x.FromTask == taskFrom) as Dependency;

                if (dependency != null)
                {
                    taskTo.RemoveDependency(dependency);
                }
            }
        }
    }
}

With this approach, by right-clicking on the Path element in the UI (the connection between two tasks), through the DataItem property of the RelationContainer class, the from and to tasks are retrieved. Then, the from task is removed from the Dependencies collection of the to task.

Applying the abovementioned approach produces the following result:

I have attached a sample project for you to test.

Regards,
Stenly
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/.

Leire
Top achievements
Rank 1
Iron
Iron
commented on 13 May 2022, 07:23 AM

Thank you so much!!!
Tags
GanttView TimeLine
Asked by
Leire
Top achievements
Rank 1
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or