DayPilot Forums

AJAX Calendar/Scheduling Controls
DayPilot » Forums » DayPilot Lite » DayPilot Lite - set color per event.

DayPilot Lite - set color per event.

Is it possible to assign a color to an event (and not to the whole calendar = all events)
thx,
Anonymous - 5/19/2008 4:36:06 PM
The API to change color of individual events is only available in DayPilot Pro. In DayPilot Lite you will have to modify the source code.
Dan Letecky - 5/19/2008 5:31:48 PM

I also wanted event-specific colors for multiple people's events. This is how I tackled adding this feature:

I added a DataColorField property so that I can define a color for each event along with the ID/Name/Start/End DataTable. Just one more column, which I selectwith a jointhe user table, where the user's color is stored, joined on the userID to which the event is assigned.

These are the DayPilot Lite files I edited:

  • DayPilotCalendarProperties.cs - Added DataColorField accessor.
  • DayPilotCalendar.cs - Added dataColorField property to class definition, Added color retrieval to viewstate code, Substituted Event.Color for the event's background-color attribute.
  • Event.cs - Added Color property.

Then just rebuild your DLL and put it in your /bin.

It's not too tough, you can do it!

Rich from IT - 3/4/2009 5:50:40 PM

Forgot to mention, I also had to edit Day.cs so that the Events created inside it have a color variable.

Rich from IT - 3/4/2009 6:32:59 PM

Could give an example of the code to add this property?

Thanks

Anonymous - 4/9/2009 3:29:08 AM

Yes, it would be very nice you someone could post some code for example, i'm trying to get it done but i'm missing...

Andrea - 5/13/2009 11:06:18 AM

Probably this is the wrong approach to do it, because i have modified a little too much code to make it work. However this modification seem to do the job, soI post it (and maybe someone will post a better approach...)

Modifiedcode to achieve the a custom duration bar color (in italic the code, in bold my added parts)

=== Day.cs ===

private void stripAndAddEvent(Event e)
{
stripAndAddEvent(e.Start, e.End, e.PK, e.Name, e.Resource, e.DurationBarColor );
}

on the last line of the above called method:

events.Add(new Event(pk, start, end, name, resource, barColor));

=== DayPilotCalendar.cs ===

In the private fields of the class added:
private string dataBarColorField;

In renderEvent(...) added the e. to access the color of the event, instead one common for all events:

576: output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(e.DurationBarColor));

in protected override void PerformDataBinding(IEnumerable retrievedData) added:

if (DataBarColorField == null || DataBarColorField == String.Empty)
throw new NullReferenceException("DataBarColorField property must be specified.");

and in the foreach loop, before items.Add

Color barColor = ColorTranslator.FromHtml(DataBinder.GetPropertyValue(dataItem, DataBarColorField, null));

=== DayPilotCalendarProperties.cs ===


[Category("Data")]
[Description("The name of the column that contains the bar color to use. ")]
public string DataBarColorField
{
get
{
return dataBarColorField;
}
set
{
dataBarColorField = value;

if (Initialized)
{
OnDataPropertyChanged();
}

}
}

=== Event.cs ===

using System.Drawing;

in the members:

public Color DurationBarColor;

public Event(string pk, DateTime start, DateTime end, string name) : this(pk, start, end, name, null)
{
this.DurationBarColor = Color.Red; // i think it is not used directly
}

public Event(string pk, DateTime start, DateTime end, string name, string resource)
{
this.PK = pk;
this.Start = start;
this.End = end;
this.Name = name;
this.Resource = resource;
this.DurationBarColor = Color.Blue; // this is the "default" color if not specified
}

/// <summary>
/// Constructor that uses the Duration Bar Color .
public Event(string pk, DateTime start, DateTime end, string name, string resource, Color durationColor)
{
this.PK = pk;
this.Start = start;
this.End = end;
this.Name = name;
this.Resource = resource;
this.DurationBarColor = durationColor;
}

=== End ==

To use it in the BindingDataTable.aspx.cs i can use something like this:

DataTable dt;

dt = new DataTable();
dt.Columns.Add("start", typeof(DateTime));
dt.Columns.Add("end", typeof(DateTime));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("barColor", typeof(string));

DataRow dr;dr = dt.NewRow();
dr["id"] = dView[i].Row["Cod"];

if (XXX)
dr["barColor"] = "red";
else
dr["barColor"] = "blue";

Andrea - 5/13/2009 2:39:15 PM

a last note:

on the .aspx page you must add the property name like this:

<DayPilot:DayPilotCalendar ID="DayPilotCalendar1" runat="server" DataEndField="end"
Days="7" StartDate="2009/05/08" DataStartField="start" DataTextField="name" DataValueField="id" DataBarColorField="barColor"
HourHeight="50" EventHoverColor="Gainsboro" ........

Andrea - 5/13/2009 2:42:22 PM
Did you get this to work for the Scheduler too? I have made the additions as you outlined and added some more to the scheduler but still can't get it to do anything but blue. :(
Laurie T - 6/16/2009 6:09:01 PM

Thanks a lot Andrea for posting the code. This helped me to reach my requirement to give different colors to different events.

Naina

Anonymous - 8/19/2009 3:05:42 PM

I made the small the changes mentioned in the above code.....but I am unabloe to reflect the changes in my events w.r.t color..

Nayan - 1/11/2010 7:12:19 PM

Assuming you are using Andrea's solution, add the following bolded text to DayPilotScheduler.cs:

First:

public class DayPilotScheduler : DataBoundControl, IPostBackEventHandler
{
internal List<Day> days;

private string dataStartField;
private string dataEndField;
private string dataTextField;
private string dataValueField;
private string dataResourceField;
private string dataBarColorField;

Second (in renderEvent):

Comment out this line: // output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(EventBackColor));

Color c = ColorTranslator.FromHtml(p.DurationBarColor);

output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(c));

Third (in #region Properties)

/// <summary>
/// Gets or sets the Name of the column that contains the bar color to /// use for events
/// </summary>
[Category("Data")]
[Description("The name of the column that contains the bar color to use for events. ")]
public string DataBarColorField
{
get
{
return dataBarColorField;
}
set
{
dataBarColorField = value;

if (Initialized)
{
OnDataPropertyChanged();
}
}
}

Fourth (in PerformDataBinding):

if (String.IsNullOrEmpty(DataBarColorField))
{
throw new NullReferenceException("DataBarColorField property must be specified.");
}

In the foreach (object dataItem in retrievedData)

string barcolor = DataBinder.GetPropertyValue(dataItem, DataBarColorField, null);

((ArrayList)ViewState["Items"]).Add(new Event(val, start, end, name, resourceId, barcolor));

This is my test data:

Dim dt As DataTable
dt = New DataTable()
dt.Columns.Add("id", GetType(String))
dt.Columns.Add("start", GetType(DateTime))
dt.Columns.Add("end", GetType(DateTime))
dt.Columns.Add("name", GetType(String))
dt.Columns.Add("barcolor", GetType(String))
dt.Columns.Add("nullfield", GetType(String))

Dim dr As DataRow
...
dr = dt.NewRow()
dr("id") = 4
dr("start") = Convert.ToDateTime("3/11/2010")
dr("end") = Convert.ToDateTime("3/13/2010")
dr("name") = "Event 4"
dr("barcolor") = "#FFE303" 'Yellow
dr("nullfield") = String.Empty
dt.Rows.Add(dr)
...

DayPilotScheduler1.DataSource = dt
DayPilotScheduler1.DataBind()

Happy coding :)

Steve Adkins - 3/21/2010 9:50:28 PM

Using the ForeColor property of the control.

Add this line to the renderEvent(Day d, Event p, HtmlTextWriter output) routine:

output.AddStyleAttribute("color", ColorTranslator.ToHtml(ForeColor));

Steve Adkins - 3/22/2010 4:03:19 PM

I follow the post to change the coding.

But all of my event display only in red. Can any one help me?

Anonymous - 4/20/2010 2:29:12 AM

Hi,

I tryed to implement different colors for my different kinds of events in an DayPilot Light Calendar but it doesn't work...

In the Day.cs class, I got two problems:

- Error 1 No overload for method 'stripAndAddEvent' takes 6 arguments C:\Users\Asphalt\.vplls\Documents\Visual Studio 2010\Projects\project\trunk\DayPilot\Source\Web\Ui\Day.cs 72 13 DayPilot

and

- Error 2 The name 'barColor' does not exist in the current context C:\Users\Asphalt\.vplls\Documents\Visual Studio 2010\Projects\project\trunk\DayPilot\Source\Web\Ui\Day.cs 106 66 DayPilot

Could you help me please. It's an important project for me.

Thanks for your help - Toni

Toni PEINOIT - 5/24/2010 12:04:17 PM
Post reply