I would like an example of how I can load tasks manually into a Gantt.... not a data binding but something similar to:
RadGantt.Task.Add(new Task());
then... RadGantt.DataBind() or something to get it to update from code behind in C#
Thanks
Tim
5 Answers, 1 is accepted
I've attached a sample page where the Gantt control is bound entirely in code behind, with implementation for every CRUD operation.
Regards,
Bozhidar
Telerik
Thank you for the response. I have looked at this example already and I have an issues more related to timing I suppose in getting this to work.
I have a search box that I use to search for a work order. I respond to the search request and pull that work order information out of the database and then set some fields on the page as part of the response.
What I don't understand are 2 things:
1. When is the Task list updated?
2. How can I manually update the Tasks list and force and update during the time that I am gathering the information from the work order from the original search request.
I tried to mimic what you did for Tasks in the segment of code where I gather the information:
List<Task> sessionTasks = Session[TasksKey] as List<Task>;
if (sessionTasks == null)
sessionTasks = new List<Task>();
else sessionTasks.Clear();
Task sr_start = new Task();
sr_start.ID = 1;
sr_start.Start = wo.ReportedDate;
sr_start.End = wo.ActualStart;
sr_start.Title = "SR Created";
sr_start.OrderID = 0;
sessionTasks.Add(sr_start);
foreach(WorkOrderHistory woh in wo.history)
{
Task t = new Task();
t.ID = 1;
t.Start = woh.Date;
t.Title = woh.Status;
sessionTasks.Add(t);
Debug.WriteLine("Added: " + woh.Status);
WorkOrderTimeLine.Tasks.Add(t);
}
Session[TasksKey] = sessionTasks;
But, it does not update the tasks... worse yet, the page freezes and crashes in Chrome.
How could I accomplish this? Or, if you can give me a better method to accomplish this I am OK with that as well.
Thanks
Tim
1. When is the Task list updated? - In the provided sample the list is stored in Session, so that the changes made on the client will be preserved upon each postback. In the sample the list itself is not updated. On Created, Delete and Update operations a server event is triggered and items are added or removed from the list.
2. If you want to reset the list, you simply have to clear the Tasks collection and add the new items. I've updated the sample project, adding a button. On button click the gantt data is cleared and a new task is added, simulating your use case.
Regards,
Bozhidar
Telerik
i got error 404, when trying to download the Gantt.zip file.
Is it stillavailable?
There is a minor issue with our server. For your convenience I'm pasting the markup and codebehind from the attached zip file:
Markup:
<
telerik:RadGantt
runat
=
"server"
ID
=
"RadGantt1"
SnapToGrid
=
"false"
OnDependencyInsert
=
"RadGantt1_DependencyInsert"
OnDependencyDelete
=
"RadGantt1_DependencyDelete"
OnTaskDelete
=
"RadGantt1_TaskDelete"
OnTaskUpdate
=
"RadGantt1_TaskUpdate"
OnTaskInsert
=
"RadGantt1_TaskInsert"
>
<
DataBindings
>
<
TasksDataBindings
IdField
=
"ID"
TitleField
=
"Title"
StartField
=
"Start"
EndField
=
"End"
PercentCompleteField
=
"PercentComplete"
OrderIdField
=
"OrderID"
SummaryField
=
"Summary"
ParentIdField
=
"ParentID"
/>
<
DependenciesDataBindings
IdField
=
"ID"
PredecessorIdField
=
"PredecessorID"
SuccessorIdField
=
"SuccessorID"
TypeField
=
"Type"
/>
</
DataBindings
>
</
telerik:RadGantt
>
<
asp:Button
ID
=
"Button1"
Text
=
"Change Data"
runat
=
"server"
OnClick
=
"Button1_Click"
/>
CodeBehind:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
Telerik.Web.UI.Gantt;
public
partial
class
Temp_GanttBinding_List : System.Web.UI.Page
{
private
const
string
TasksKey =
"Telerik.GanttSample.Tasks"
;
private
const
string
DependenciesKey =
"Telerik.GanttSample.Dependencies"
;
private
List<Task> Tasks
{
get
{
List<Task> sessionTasks = Session[TasksKey]
as
List<Task>;
if
(sessionTasks ==
null
)
{
sessionTasks =
new
List<Task>();
sessionTasks.Add(
new
Task { ID = 1, Title =
"First Task"
,
Start =
new
DateTime(2014, 8, 5, 9, 10, 0), End =
new
DateTime(2014, 8, 5, 12, 40, 0), OrderID = 0 });
Session[TasksKey] = sessionTasks;
}
return
sessionTasks;
}
}
public
List<Dependency> Dependencies
{
get
{
List<Dependency> sessionDependencies = Session[DependenciesKey]
as
List<Dependency>;
if
(sessionDependencies ==
null
)
{
sessionDependencies =
new
List<Dependency>();
Session[DependenciesKey] = sessionDependencies;
}
return
sessionDependencies;
}
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
RadGantt1.DataSource = Tasks;
RadGantt1.DependenciesDataSource = Dependencies;
}
protected
void
RadGantt1_TaskDelete(
object
sender, TaskEventArgs e)
{
foreach
(Task task
in
e.Tasks)
{
var original = GetByID((
int
)task.ID);
Tasks.Remove(original);
}
}
protected
void
RadGantt1_TaskUpdate(
object
sender, TaskEventArgs e)
{
foreach
(Task task
in
e.Tasks)
{
var original = GetByID((
int
)task.ID);
original.Start = task.Start;
original.End = task.End;
original.Title = task.Title;
original.PercentComplete = task.PercentComplete;
original.OrderID = task.OrderID;
original.Summary = task.Summary;
original.ParentID = task.ParentID;
}
}
protected
void
RadGantt1_TaskInsert(
object
sender, TaskEventArgs e)
{
foreach
(Task task
in
e.Tasks)
{
task.ID = NextID();
Tasks.Add(task);
}
}
private
Task GetByID(
int
id)
{
return
Tasks.Find((task) => (
int
)task.ID == id);
}
private
int
NextID()
{
var nextID = 0;
foreach
(ITask task
in
Tasks)
{
if
((
int
)task.ID > 0)
{
nextID = (
int
)task.ID;
}
}
return
++nextID;
}
protected
void
RadGantt1_DependencyInsert(
object
sender, DependencyEventArgs e)
{
foreach
(Dependency dependency
in
e.Dependencies)
{
dependency.ID = NextDependencyID();
Dependencies.Add(dependency);
}
}
protected
void
RadGantt1_DependencyDelete(
object
sender, DependencyEventArgs e)
{
foreach
(Dependency dependency
in
e.Dependencies)
{
var original = GetDependencyByID((
int
)dependency.ID);
Dependencies.Remove(original);
}
}
private
Dependency GetDependencyByID(
int
id)
{
return
Dependencies.Find((dependency) => (
int
)dependency.ID == id);
}
private
int
NextDependencyID()
{
var nextID = 0;
foreach
(IDependency dependency
in
Dependencies)
{
if
((
int
)dependency.ID > 0)
{
nextID = (
int
)dependency.ID;
}
}
return
++nextID;
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Tasks.Clear();
Tasks.Add(
new
Task
{
ID = 1,
Title =
"New Task"
,
Start =
new
DateTime(2014, 8, 5, 9, 10, 0),
End =
new
DateTime(2014, 8, 5, 12, 40, 0),
OrderID = 0
});
}
}
Regards,
Bozhidar
Telerik by Progress