This is a migrated thread and some comments may be shown as answers.

RadCalendar Adding/Removing Dates

4 Answers 257 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 18 Jul 2011, 09:18 PM
Hi there,

I have been using the RadCalendar on my webpage.

RadControls for ASP.NET AJAX Q1 2011
version 2011.1.413.35
on a .net 3.5 website

Adding a selected date is easy enough:
        RadCalendar1.SelectedDates.Add(new RadDate(new DateTime(2011, 7, 28)));

Removing a previously added selected date gives an error, however:
        RadCalendar1.SelectedDates.Remove(new RadDate(new DateTime(2011, 7, 28)));
 
Error:
Cannot remove the specified item because it was not found in the specified Collection.
despite the fact that it was added.

Or if I do something like this, it Contains comes back as true, and then remove comes up with the same error.

if (RadCalendar1.SelectedDates.Contains(new RadDate((DateTime)item["CourseDate"]))) {
    RadCalendar1.SelectedDates.Remove(new RadDate((DateTime)item["CourseDate"]));
}

Any ideas?

4 Answers, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 21 Jul 2011, 02:45 PM
Hello John,

Thank you for contacting us.

Yes the day is the same. But the object is different.

So this for example will work:
RadDate day = new RadDate(new DateTime(2011, 7, 28));
RadCalendar1.SelectedDates.Add(day);
RadCalendar1.SelectedDates.Remove(day);

This will also works even if the day1 and day2 have the same date.
RadDate day1 = new RadDate(new DateTime(2011, 7, 28));
RadDate day2 = new RadDate(new DateTime(2011, 7, 28));
 
RadCalendar1.SelectedDates.Add(day1);
RadCalendar1.SelectedDates.Add(day2);
RadCalendar1.SelectedDates.Remove(day1);
RadCalendar1.SelectedDates.Remove(day2);

However, this one will not work even if the day1 and day2 still have the same date:
RadDate day1 = new RadDate(new DateTime(2011, 7, 28));
RadDate day2 = new RadDate(new DateTime(2011, 7, 28));
 
RadCalendar1.SelectedDates.Add(day1);
RadCalendar1.SelectedDates.Add(day2);
 
RadCalendar1.SelectedDates.Remove(day1);
RadCalendar1.SelectedDates.Remove(day1);

The Contains will find such item because it returns True if the same DateTime exist.

So you could iterate thought the collection and remove given day like in the code below:
RadCalendar1.SelectedDates.Add(new RadDate(new DateTime(2011, 7, 28)));
 
DateTime dayTime = new DateTime(2011, 7, 28);
 
 
for (int i = 0; i < RadCalendar1.SelectedDates.Count; i++)
{
    if (((RadDate)RadCalendar1.SelectedDates[i]).Date == dayTime)
    {
        RadCalendar1.SelectedDates.RemoveAt(i);
        break;
    }
}

Our developers will consider further for defining EqualityComparer for the RadDate class.

All the best,
Vasil
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
CJ
Top achievements
Rank 1
answered on 16 Dec 2011, 09:22 AM
Hello,
When i click on column header of the radcalendar it selects all the days of week for that month, ie. When i click on 'W' then it selects all the Wednesdays for that month. But what i want is when i click on 'W' then it should select all the Wednesdays for an entire date range (range i have set using rangemindate and rangemaxdate span across 10 months) after asking a confirmation

For this i have done the following
string day = "Wednesday"; // i am setting this value dynamically through client side OnColumnHeaderClick
for (DateTime date = rangeStart; date.CompareTo(rangeEnd) < 1; date = date.AddDays(1))
                {
                    if (day == Enum.GetName(typeof(DayOfWeek), date.DayOfWeek)) 
                    {
                        RadDate rd = new RadDate(date);
                        radCalendarAM.SelectedDates.Add(rd);
                    }
                }
This works perfectly when selecting dates. Now if i click on the same column header again i want to remove all the "wednesdays" for the entire date range. To remove i have done the following as suggested in this thread
DayOfWeek dow = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), day);
                for (int i = 0; i < radCalendarAM.SelectedDates.Count; i++)
                {
                    RadDate date = (RadDate)radCalendarAM.SelectedDates[i];
                    if (date.Date.DayOfWeek == dow)
                    {
                        radCalendarAM.SelectedDates.RemoveAt(i);
                    }
                }

But this code does'nt remove all the Wednesdays in the date range, it leave behind few wednesdays (or which ever day) selected.
Can you advice on this?

-thanks



0
Vasil
Telerik team
answered on 16 Dec 2011, 04:21 PM
Hello Cj,

This is because when element of the array is removed you jump with one item and not check the next one.

Here is an example using some pseudo-code:
array = [true, true, false,true];
index = 0;
if (array[0])
   array.removeAt(0);
//array = [true, false, true];
index = 1;
if (array[1]) //here array[1] is false;
   array.removeAt(1);
//array = [true,false, true]
 
if (array[2])
   array.removeAt(2);
//array = [true,false]
So in the code above we tried to remove all elements that are true, but we was unable to because of the wrong incrementing of the "index".

You have two options to resolve this.
1) Decrement the 'i' with one when remove an element.
2) Loop the SelectedDates in reverse order, starting from the last one and ending with the first.

I hope this helps.

All the best,
Vasil
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
CJ
Top achievements
Rank 1
answered on 16 Dec 2011, 06:08 PM
Thank you Vasil,

Option 1. Decrement the 'i' with one when remove an element worked out just fine.

thanks
CJ

Tags
Calendar
Asked by
John
Top achievements
Rank 1
Answers by
Vasil
Telerik team
CJ
Top achievements
Rank 1
Share this question
or