MindFusion.Scheduling for ASP.NET Programmer's Guide
Tutorial 2: Filtering and grouping

This tutorial shows how to programmatically filter and group appointments using MindFusion.Scheduling for ASP.NET.

1. Initial preparations

Read Adding the control tutorial for detailed instructions on how to create a web site and add the Calendar control to it. Follow the steps in Tutorial 1: Creating appointments to add contacts, locations and appointments.

2. Filtering the Calendar

For the purpose of this tutorial add a ListBox control and three buttons to the page. Set the ListBox's ID to 'lbContacts' and its SelectionMode property to 'Multiple'. Set the buttons' Text property to 'Apply filter', 'Apply grouping' and 'Clear grouping/filtering'.

Switch to code view and add the following code at the end of CreateScheduleData method:

C#  Copy Code

//fill the list of contacts
lbContacts.Items.Clear();
lbContacts.Items.Add(new ListItem(contact1.LastName + ", " + contact1.FirstName));
lbContacts.Items.Add(new ListItem(contact2.LastName + ", " + contact2.FirstName));
lbContacts.Items[0].Selected = true;

Visual Basic  Copy Code

'fill the list of contacts
lbContacts.Items.Clear()
lbContacts.Items.Add(New ListItem(contact1.LastName + ", " + contact1.FirstName))
lbContacts.Items.Add(New ListItem(contact2.LastName + ", " + contact2.FirstName))
lbContacts.Items(0).Selected = True

This code adds the contacts, created in Tutorial1 to the ListBox control. We will use them to select by which resource(s) the Calendar will be filtered or grouped.

Double click on the first (Apply filter) button and in the code-behind add the following code to its Click handler:

C#  Copy Code

//clear the contacts
Calendar1.Contacts.Clear();

//add the selected contacts to the Calendar's Contacts collection
for (int i = 0; i < lbContacts.Items.Count; i++)
{
    var li = lbContacts.Items[i];
    if (li.Selected)
    {
        Contact c = Calendar1.Schedule.Contacts[i];
        if (!Calendar1.Contacts.Contains(c))
            Calendar1.Contacts.Add(c);
    }
}

//apply filter
Calendar1.GroupType = GroupType.FilterByContacts;

Visual Basic  Copy Code

'clear the contacts
Calendar1.Contacts.Clear()

If lbContacts.SelectedIndex <> -1 Then

    'add the selected contacts to the Calendar's Contacts collection
    For i As Integer = 0 To lbContacts.Items.Count - 1
        Dim li = lbContacts.Items(i)
        If li.Selected Then
            Dim c As Contact = Calendar1.Schedule.Contacts(i)
            If Not Calendar1.Contacts.Contains(c) Then
                Calendar1.Contacts.Add(c)
            End If
        End If
    Next

    'apply filter
    Calendar1.GroupType = GroupType.FilterByContacts

End If

This code gets all contacts, which are selected in the ListBox and adds them to the Calendar's Contacts collection. Finally it sets the GroupType property of the Calendar to GroupType.FilterByContacts.

3. Grouping the Calendar

Double click on the second (Apply grouping) button and in the code-behind add the following code to its Click handler:

C#  Copy Code

//clear the contacts
Calendar1.Contacts.Clear();

//add the selected contacts to the Calendar's Contacts collection
for (int i = 0; i < lbContacts.Items.Count; i++)
{
    var li = lbContacts.Items[i];
    if (li.Selected)
    {
        Contact c = Calendar1.Schedule.Contacts[i];
        if (!Calendar1.Contacts.Contains(c))
            Calendar1.Contacts.Add(c);
    }
}

//apply grouping
Calendar1.GroupType = GroupType.GroupByContacts;

Visual Basic  Copy Code

'clear the contacts
Calendar1.Contacts.Clear()

'add the selected contacts to the Calendar's Contacts collection
For i As Integer = 0 To lbContacts.Items.Count - 1

    Dim li = lbContacts.Items(i)
    If li.Selected Then
        Dim c As Contact = Calendar1.Schedule.Contacts(i)
        If Not Calendar1.Contacts.Contains(c) Then
            Calendar1.Contacts.Add(c)
        End If
    End If

Next

'apply grouping
Calendar1.GroupType = GroupType.GroupByContacts

This code gets all contacts, which are selected in the ListBox and adds them to the Calendar's Contacts collection. Finally it sets the GroupType property of the Calendar to GroupType.GroupByContacts.

4. Clear all grouping and filtering

Double click on the last (Clear grouping/filtering) button and in the code-behind add the following code to its Click handler:

C#  Copy Code

Calendar1.Contacts.Clear();
Calendar1.GroupType = GroupType.None;

Visual Basic  Copy Code

Calendar1.Contacts.Clear()
Calendar1.GroupType = GroupType.None

The code above will remove any existing filtering or grouping by setting the Calendar's GroupType property to GroupType.None.

5. Build and run

Run the web page. The image below depicts what the page output would look like when the Calendar is grouped by both contacts: