Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1080
Delete task and all its subtasks - databinding error
posted

Hi, I can't figure out how to delete all subtask for task, and save it to database. I made this recursive function, which delete tasks from gantview, but allways throws exception when saving to database

 private void deleteTasksAndSubtasks(Task task)
        {
            if (task.Tasks.Count > 0)
            {
                for (int i = 0; i < task.Tasks.Count; i++)
                {
                    Task taskF = task.Tasks[i];

                    if (taskF.IsSummary == true)
                    {
                        for (int j = 0; j < taskF.Tasks.Count; j++)
                            this.deleteTasksAndSubtasks(taskF.Tasks[j]);

                        task.Tasks.Remove(taskF);
                    }
                    else
                    {
                        task.Tasks.Remove(taskF);
                    }
                }
            }
        }

And I call this function like this

 private void ultraGanttView1_TaskDeleting(object sender, TaskDeletingEventArgs e)
        {
            if (e.Task.IsSummary)
            {
                 deleteTasksAndSubtasks(e.Task);  saveTasks();
            }
        }

It can delete task and his subtask, but if subtask has its subtask, ie if it has more than 2 levels, it always throws exception. What could be the problem ?

And I have another question. When I right click on ganttview and in toolstrip click on "Add task below selected task" in database it saves changes, but it doesn't write Project Key for this subtask I created. So next time i reload project, task is not in list, but its in database. and Project Key value is null. I tried to set Project Key value in ultraGanttView1_TaskAdded function, like this

e.Task.Project = ultraCalendarInfo6.Projects[1];

but it doesn't write project key to database. Any solution for this ?

Parents
  • 1080
    Offline posted

    I made some changes in upper function.

    private void deleteTasksAndSubtasks(Task task)
            {
                bool deleted = false;

                for (int i = task.Tasks.Count; i > 0; i--)

                {
                    Task taskF = task.Tasks[i - 1];

                    if (taskF.IsSummary == true)
                    {
                        deleteTasksAndSubtasks(taskF);
                        deleted = false;
                    }
                    else
                    {
                        task.Tasks.Remove(taskF);
                        deleted = true;
                    }

                    if (deleted == false)
                    {
                        task.Tasks.Remove(taskF);
                    }
                }
            }

    and it deletes everything except the root task, which I click on, and one of its children. If I use message boxes everytime I remove Task, I see that all tasks are deleted, but not in the database. Still no luck, but I am closer.

Reply Children
No Data