positive perspective
positive perspective

Reputation: 349

Unable to get Date from List and set SelectedDates programmaticaly in CalendarView

I have a Table that looks like this:

class Calendar
{
    [SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
    public int Id { get; set; }
    public string PersonId { get; set; }
    public string PlaceId { get; set; }
    public string Date {get; set;}

    public Calendar()
    {

    }
    public Calendar(string personId, string placeId, string date)
    {
        PersonId = personId;
        PlaceId = placeId;
        Date = date;
    }
}

I also have a CalendarView:

            <CalendarView NumberOfWeeksInView="6" SelectedDatesChanged="MyCalendarView_DateChanged" x:Name="MyCalendarView" DisplayMode="Month" SelectionMode="Multiple"/>

What I am trying to do is to get the CalendarView markSelectedDates based on the data from Table. I want it to select the date that corresponds to the Date stored in the Calendar Table.

Lets say, I want the CalendarView to select all dates that are corresponding to PersonId == "James" and PlaceId == "Tokyo".

Here is what I have so far:

private void VerifyButton_Click(object sender, RoutedEventArgs e)
    {

        ReadAllCalendarList dbcalendar = new ReadAllCalendarList();
        DB_CalendarList = dbcalendar.GetAllCalendar();
        var calendarQuery = DB_CalendarList.Where(
        Calendar => Calendar.PersonId == PersonSelector.SelectedItem.ToString() &&
        Calendar.PlaceId == SelectedPlaceName.Text.Trim());

(the ListView below actually shows all records that match the query above, so the query works)

        LstViewCalendar.ItemsSource = (calendarQuery.OrderByDescending(i => i.Id).ToList());

Now, if I place here this code just for testing:

MyCalendarView.SelectedDates.Add(DateTimeOffset.Now);

IT WORKS. and the CalendarView selects the todays date.

So I continue with the code as follows:

        List<Calendar> calendarRecords = new List<Calendar>();
        //something wrong with foreach below or List above
        foreach (var date in calendarRecords)
        {

            MyCalendarView.SelectedDates.Add(DateTimeOffset.Parse(date.Date));
            MyCalendarView.SelectedDates.Add(DateTimeOffset.Now);
        }

    }

And NONE of the two statements in foreach above works. The List above is not sorted, it lists all records from the table. I was testing it this way but it doesn't work. Also, here is how I add records to Calendar Table:

for (int i = 0; i <= MyCalendarView.SelectedDates.Count - 1; i++)
            {
                Db_Helper.InsertCal(new Calendar(
                Convert.ToString(PersonSelector.SelectedItem),          
                (SelectedPlaceName.Text.Trim()),                                                             
                MyCalendarView.SelectedDates[i].ToString("dd-MM-yyyy")
                ));
            }

Any help please? Basically nothing I would put inside the foreach would work. I am doing something wrong while iterating through the Calendar class records but don't know what.

Upvotes: 0

Views: 58

Answers (1)

positive perspective
positive perspective

Reputation: 349

Ok, I have sorted it out.

basically, the List was empty. I have assigned my query to the list like this:

List<Calendar> calendarRecords = new List<Calendar>();
calendarRecords = calendarQuery.ToList();

and the:

MyCalendarView.SelectedDates.Add((date.Date));

works. but there was another change I had to make: In Calendar class I have changed string Date to DateTimeOffset Date.

This way I have managed to SelectDates programmatically in CalendarView using data contained in my SQLite database.

Upvotes: 2

Related Questions