0

Got the error info as title shows. But what I want is reading one row from datatable, insert into my calendar control, then delete this row, and read next row, insert, then delete the next.

But I got this error

Below is my code

for (int i = 0; i < dtRowCount; i++)
        {
            var tempName = tempDT.Rows[i]["IDENT"].ToString();

            c = new Contact();
            c.FirstName = tempDT.Rows[0]["IDENT"].ToString();
            c.Tag = tempDT.Rows[0]["EVENT_ID"].ToString();
            _calendar.Schedule.Contacts.Add(c);
            _calendar.Contacts.Add(c);

            for (int j = 0; j < dtRowCount; j++)
            {
                if (tempName == tempDT.Rows[j]["TAG_IDENT"].ToString())
                {
                    #region Appointment

                    var app = new Appointment();

                    app.HeaderText = tempDT.Rows[i]["NAME"].ToString();

                    exlStarTime = tempDT.Rows[i]["START"].ToString();
                    DateTime myStartDate = DateTime.ParseExact(exlStarTime,
                        "dd-MMM-yy hh.mm.ss.fffffff00 tt",
                        System.Globalization.CultureInfo.InvariantCulture);

                    exlEndTime = tempDT.Rows[i]["END"].ToString();
                    DateTime myEndDate = DateTime.ParseExact(exlEndTime,
                        "dd-MMM-yy hh.mm.ss.fffffff00 tt",
                        System.Globalization.CultureInfo.InvariantCulture);

                    app.Tag = tempDT.Rows[i]["TYPE_CODE"].ToString();
                    app.StartTime = myStartDate;
                    app.EndTime = myEndDate;

                    app.Contacts.Add(c);
                    _calendar.Schedule.Items.Add(app);

                    ItemStyle style = app.Style;
                    style.HeaderTextColor = Color.White;
                    style.Brush.Image = "none";
                    switch (tempDT.Rows[i]["EVENT_TYPE_CODE"].ToString())
                    {
                        case event1Color:
                            style.Brush.Color = Color.OrangeRed;
                            break;

                        case event2Color:
                            style.Brush.Color = Color.Orange;
                            break;

                        case event3Color:
                            style.Brush.Color = Color.DodgerBlue;
                            break;
                    }

                    #endregion

                    tempDT.Rows[j].Delete();
                    //j = j - 1;
                    j = j + 1;
                }
            }
        }

big thx

Windtalker
  • 776
  • 4
  • 13
  • 23

2 Answers2

1

I think the problem is due to the fact you're modifying the Collection you're iterating through. The easiest way to fix it is to do it in two steps. First you insert all you need in the Calendar, then after being done with the insertions you start deleting them.

Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
  • Coz for each contact I may have multiple appointment time range, exp: Tom has appointment A from May to June, and appointment B from Aug to Oct. In case loading them duplicate value in the loop, I need to delete the row after insert it into calendar – Windtalker Mar 05 '14 at 16:56
  • I don't fully understand the issue at hand, but I more or less think that if you have the issue of tracking which appointments you've already processed and which ones you haven't, you could use a variable for each appointment to flag if that appointment was processed or not. – Saverio Terracciano Mar 05 '14 at 17:09
  • Let's discuss it in another way, how can I detect whether specified row has been deleted or not? I tried to use "tempDT.Rows[i].IsNull("TAG_IDENT")", but if this row had been deleted, error info: "Deleted row information cannot be accessed through the row" will pop up coz "tempDT.Rows[i]" is not existing. – Windtalker Mar 05 '14 at 19:17
  • Once a row has been deleted you can't of course know it existed, *unless* you have some other data structure that memorized the previous status. As example you could keep an array of classes class deletedRow { string idRow; bool deleted; } – Saverio Terracciano Mar 05 '14 at 19:29
  • Aha I solved that, simply put an array recording each delete action, put the deleted row number in the list. So once loops to the deleted row, a condition will be invoked to jump to next row. – Windtalker Mar 06 '14 at 17:39
0

This is because you need to invoke to AcceptChanges() method after you deleted all rows.

tempDT.AcceptChanges();

I hope this useful for you!

Manolo
  • 31
  • 2