New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Using the Data Source Property

RadScheduler has a DataSource property that lets you bind it to any object that implements the IListSource or IEnumerable interface. Thus, you can bind the scheduler directly to a DataTable or DataView (IListSource), or to various List objects (IEnumerable).

When using the DataSource property to bind the scheduler, you must also implement the code for inserting, updating, and deleting appointments using the RadScheduler server-side events. A sample implementation can be found in the Scheduler bound to a List using the DataSource property Code Library project.

If you are using custom fields/attributes, you must write them directly to the data source in these events, so you can later access them in the AppointmentDataBound event and either use them for your logic, or add them to the Attributes collection of the appointment.

In the AppointmentDataBound event, you must check whether the DataItem of the appointment has been initialized before accessing it, for example:

C#
protected void RadScheduler1_AppointmentDataBound(object sender, SchedulerEventArgs e)
{
	if (!object.Equals(e.Appointment.DataItem, null))
	{
		//if using a custom class
		string custAttr = (e.Appointment.DataItem as MyCustomAppointmentInfo).CustomAttr;
		//if using a generic object like DataTable
		string data = (e.Appointment.DataItem as System.Data.DataRowView)["CustomAttr"].ToString();
	}
}

Example

The following example demonstrates binding to a generic list of objects, which represents appointment information, including a custom resource for room. It implements AppointmentInsert, AppointmentUpdate, and AppointmentDelete event handlers to allow the scheduler to insert, update, and delete appointments.

  1. Drag a RadScheduler control from the toolbox onto your page.

  2. Move to the code-behind page for your web page, and add two lines to the using section (C#) or Imports section (VB).

C#
using Telerik.Web.UI;
using System.Collections.Generic;
  1. In the definition of the class for your Web page, add two class definitions, one to hold information about appointments, and another to hold information about the custom resource (rooms):
C#
class AppointmentInfo
{
	private string id;
	private string subject;
	private DateTime start;
	private DateTime end;
	private string recurParentID;
	private string recurData;
	private int room;

	public string ID
	{
		get { return id; }
		set { id = value; }
	}
	public string Subject
	{
		get { return subject; }
		set { subject = value; }
	}
	public DateTime Start
	{
		get { return start; }
		set { start = value; }
	}
	public DateTime End
	{
		get { return end; }
		set { end = value; }
	}
	public string RecurrenceRule
	{
		get { return recurData; }
		set { recurData = value; }
	}
	public string RecurrenceParentID
	{
		get { return recurParentID; }
		set { recurParentID = value; }
	}
	public int RoomNo
	{
		get { return room; }
		set { room = value; }
	}

	private AppointmentInfo()
	{
		this.id = Guid.NewGuid().ToString();
	}
	public AppointmentInfo(string subject, DateTime start, DateTime end): this()
	{
		this.subject = subject;
		this.start = start;
		this.end = end;
	}
	public AppointmentInfo(Appointment source): this()
	{
		CopyInfo(source);
	}

	public void CopyInfo(Appointment source)
	{
		subject = source.Subject;
		start = source.Start;
		end = source.End;
		recurData = source.RecurrenceRule;
		if (source.RecurrenceParentID != null)
			recurParentID = source.RecurrenceParentID.ToString();
		Resource r = source.Resources.GetResourceByType("Room");
		if (r != null)
			room = (int) r.Key;
	}
}

class RoomInfo
{
	private int id;
	private string name;

	public int RoomNo
	{
		get { return id; }
	}

	public string RoomName
	{
		get { return name; }
	}

	public RoomInfo(int id, string name)
	{
		this.id = id;
		this.name = name;
	}
} 
  1. Add two private properties to the class for your Web page whose values are the current list of appointments and the list of rooms. The appointments list needs to persist, so we save that one in the session state:
C#
private const string AppointmentsKey = "Telerik.Examples.Scheduler.BindToList_Apts";

private List<AppointmentInfo> Appointments
{
    get
    {
        List<AppointmentInfo> sessApts = Session[AppointmentsKey] as List<AppointmentInfo>;
    
        if (sessApts == null)
        {
            sessApts = new List<AppointmentInfo>();
            Session[AppointmentsKey] = sessApts;
        }

        return sessApts;
    }
}

private List<RoomInfo> Rooms
{
    get
    {
        List<RoomInfo> roomList = new List<RoomInfo>();

        roomList.Add(new RoomInfo(1, "Margaret Morrison Main Room"));
        roomList.Add(new RoomInfo(2, "Black Auditorium"));
        roomList.Add(new RoomInfo(3, "Doherty Auditorium"));

        return roomList;
    }
} 
  1. Add the FindById helper function to the class for your Web page:
C#
private AppointmentInfo FindById(string ID)
{
    foreach (AppointmentInfo ai in Appointments)
    {
        if (ai.ID == ID)
        {
            return ai;
        }
    }

    return null;
} 
		
  1. In the Page_Load event handler, add code to initialize the RadScheduler so that it can read the appointment information, define the custom resource type for rooms, and set the DataSource property to bind the scheduler to the data.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session.Remove(AppointmentsKey);

        RadScheduler1.DataKeyField = "ID";
        RadScheduler1.DataStartField = "Start";
        RadScheduler1.DataEndField = "End";
        RadScheduler1.DataSubjectField = "Subject";
        RadScheduler1.DataRecurrenceField = "RecurrenceRule";
        RadScheduler1.DataRecurrenceParentKeyField = "RecurrenceParentID";

        ResourceType rt = new ResourceType("Room");

        rt.DataSource = Rooms;
        rt.KeyField = "RoomNo";
        rt.ForeignKeyField = "RoomNo";
        rt.TextField = "RoomName";

        RadScheduler1.ResourceTypes.Add(rt);
    }

    RadScheduler1.DataSource = Appointments;
} 
  1. Return to the designer for Default.aspx and select the RadScheduler control.

  2. In the properties pane, double-click on the AppointmentInsert event and add the following event handler:

C#
protected void RadScheduler1_AppointmentInsert(object sender, SchedulerCancelEventArgs e)
{
	Appointments.Add(new AppointmentInfo(e.Appointment));
} 
  1. Double-click on the AppointmentUpdate event and add the following event handler:
C#
protected void RadScheduler1_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e)
{
	AppointmentInfo ai = FindById(e.ModifiedAppointment.ID.ToString());
	ai.CopyInfo(e.ModifiedAppointment);
} 
  1. Double-click on the AppointmentDelete event and add the following event handler:
C#
protected void RadScheduler1_AppointmentDelete(object sender, SchedulerCancelEventArgs e)
{
	Appointments.Remove(FindById(e.Appointment.ID.ToString()));
} 

You have now bound your scheduler using the DataSource property. Run the application andnote the custom resource is available in the edit form.

See Also

In this article
ExampleSee Also
Not finding the help you need?
Contact Support