search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Changing the Color performance

Asked by Angel
12 years ago.

protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
{
DataTable res;
for (int i = 0; i < res.Rows.Count; i++)
{
int day;
TimeSpan start, End;
start = TimeSpan.Parse(res.Rows[i][0].ToString());
End = TimeSpan.Parse(res.Rows[i][1].ToString());
day = int.Parse(res.Rows[i][2].ToString()) - 2 < 0 ? int.Parse(res.Rows[i][2].ToString()) + 5 : int.Parse(res.Rows[i][2].ToString()) - 2;

if (e.Start.TimeOfDay >= start && e.Start.TimeOfDay < End && (int)e.Start.DayOfWeek == day)
{
e.IsBusiness = true;
e.BackgroundColor = "Teal";
}
else
e.IsBusiness = false;
}
}
it takes long time to color ?!!!!

Answer posted by Richard
12 years ago.

Your code won't compile, since you never assign the variable "res" to anything. It also won't work, since only the cells matching the last row in the table will be set to business hours.

If you're loading "res" from the database, you should load it in the calendar's DataBinding event and store it in a private field, rather that loading it for every cell in your calendar. It would also be better to parse the values at the start, rather than parsing the same values for every cell.

Assuming you only have one set of business hours per day, then something like this should work:

private Dictionary<int, Func<TimeSpan, bool>> _res;

protected void DayPilotCalendar1_DataBinding(object sender, EventArgs e)
{
DataTable res = ...; // TODO: Insert your code to load the table here

_res = new Dictionary<int, Func<TimeSpan, bool>>(res.Rows.Count);

for (int i = 0; i < res.Rows.Count; i++)
{
TimeSpan start = TimeSpan.Parse(res.Rows[i][0].ToString());
TimeSpan end = TimeSpan.Parse(res.Rows[i][1].ToString());

int day = int.Parse(res.Rows[i][2].ToString());
day = (day - 2 < 0) ? day + 5 : day - 2;

_res[day] = time => start <= time && time <= end;
}
}

protected void DayPilotCalendar1_BeforeCellRender(object sender, BeforeCellRenderEventArgs e)
{
Func<TimeSpan, bool> predicate;
if (_res.TryGetValue((int)e.Start.DayOfWeek, out predicate) && predicate(e.Start.TimeOfDay))
{
e.IsBusiness = true;
e.BackgroundColor = "Teal";
}
else
{
e.IsBusiness = false;
}
}

Comment posted by Anonymous
12 years ago.

Thank you very much, most of the code works fine :)

but i want to ask what this statement do ?!!
_res[day] = time => start <= time && time <= end;

Comment posted by Richard
12 years ago.

OK, let's break it down:

_res is a dictionary with the day number as the key, and a Func<TimeSpan, bool> delegate as the value;

A Func<TimeSpan, bool> delegate is a method which takes a TimeSpan and returns a bool;

"_res[day] = ...;" stores the delegate in the dictionary for the specified day;

"time => ..." is a lambda expression, which declares an anonymous delegate with a single parameter called "time";

The body of the delegate is "start <= time && time <= end": the delegate returns true if the captured "start" time is less than or equal to the parameter, and the parameter is less than or equal to the captured "end" time;

The delegate could also be written as any of the following:

(TimeSpan time) => start <= time && time <= end;
(TimeSpan time) => { return start <= time && time <= end; }
delegate(TimeSpan time) { return start <= time && time <= end; }

The MSDN documentation on Lambda expressions is here:
http://msdn.microsoft.com/en-us/library/bb397687.aspx

There's a CodeProject article which describes delegates, anonymous methods and lambda expressions here:
http://www.codeproject.com/Articles/47887/C-Delegates-Anonymous-Methods-and-Lambda-Expressio

Comment posted by Angel
12 years ago.

Thank you very much

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.