Hello
How to insert a task above the active task on a button click in winganttview?
Regards
Jeni
Hello Jeni,
To achieve desired functionality we could split this task in a two parts. The first one is "How to set custom order for all tasks in the GantView control" and the second one is "How to insert our new task before our Active Task"
To solved the first part of our task, we could create a new column in the database`s table. In my sample this new column has a name - ID with dataType Integer. By default the order of the tasks depend from the primary key - TaskId column with datatype uniqueidentifier. If we are using Guid will be difficult for us to set desired order for our tasks, that`s why I`m using additional Integer column.
I`ll use this column to order all Tasks in the UltraGantView control. To achieve this behavior we should modify also our Select command in the TableAdapter with "Order By ID" clause (take a look at the attached video file for more details).
The second part of our task is to reorder all previos tasks when we create a new Task. To solve this, I`m using StoredProcedure in the database with two imput parameters. The first parameter is our Active Task ID, and second parameter is the guid of our new task. To call our stored procedure, I`m using ultraGanttView1_TaskAdded() event. For example:
private void ultraGanttView1_TaskAdded(object sender, Infragistics.Win.UltraWinGanttView.TaskAddedEventArgs e)
{
SaveChangesInDataBase();
this.Reorder(ultraGanttView1.ActiveTask.RowNumber, e.Task.Id);
dbTasksTableAdapter1.ClearBeforeFill =true;
dbTasksTableAdapter1.Fill(testDataSet.dbTasks);
}
private void SaveChangesInDataBase()
dbTasksTableAdapter1.Update(testDataSet);
private void Reorder(int ActiveTaskId, Guid NewTaskGuid)
SqlConnection connection = new SqlConnection("Data Source=IGBGSOFDS22;Initial Catalog=Test;Integrated Security=True");
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 30;
command.CommandText = "sp_ReorderTasks";
command.Parameters.Add("@RC", SqlDbType.Int);
command.Parameters["@RC"].Direction = ParameterDirection.ReturnValue;
command.Parameters.Add("@iActiveTask", SqlDbType.Int);
command.Parameters.Add("@uNewTask", SqlDbType.UniqueIdentifier);
command.Parameters["@iActiveTask"].Direction = ParameterDirection.Input;
command.Parameters["@uNewTask"].Direction = ParameterDirection.Input;
command.Parameters["@iActiveTask"].Value = ActiveTaskId;
command.Parameters["@uNewTask"].Value = NewTaskGuid;
command.ExecuteNonQuery();
connection.Close();
Here is also the stored procedure that i`m using :
ALTER PROCEDURE [dbo].[sp_ReorderTasks]
@iActiveTask int,
@uNewTask uniqueidentifier
AS
DECLARE @TaskID uniqueidentifier
DECLARE @ID int
BEGIN
DECLARE q1 CURSOR FOR SELECT TaskID, ID FROM dbTasks WHERE ID >= @iActiveTask
OPEN q1
FETCH NEXT FROM q1 INTO @TaskID, @ID
WHILE @@FETCH_STATUS = 0
UPDATE dbTasks SET ID = (@ID +1) WHERE TaskID = @TaskID
END
CLOSE q1
DEALLOCATE q1
UPDATE dbTasks SET ID = @iActiveTask WHERE TaskID = @uNewTask
Please take a look at attached video file and let me know if you have any questions.
Part 2