Link to home
Start Free TrialLog in
Avatar of NewMom2Brandon
NewMom2Brandon

asked on

Question regarding Foreach loop with Listview Items

Here is my problem. I am sure it is probably something simple.

Say my listview has 6 items in it.

The problem I am running into is the whole last item #5 or 6 is not getting written out to the XML if I delete either #5 or 6 from the listview.  If I delete item 1-4 it works fine. When I delete 5 or 6 then the whole last block does not get written out.

The way my XML file should work is item 1+2 contain the same info minus 3 fields so they are linked as one. They are all paired up 3+4 and 5+6 What should happen is if I delete #5 the XML should show 1+2, 3+4, and 5 alone.

Can someone tell me what I am doing wrong in this loop. I have been working on it for a while.

         string[] PreviousRowArray = null;

         foreach (ListViewItem row in listViewFount.Items)
         {
            string CurrentRow = "";

            for (int i=0 ; i<row.SubItems.Count ;i++)
            {
               CurrentRow = CurrentRow + row.SubItems[i].Text + " ";
            }

            string[] CurrentRowArray = CurrentRow.Split(' ');
               
            if (PreviousRowArray != null)
            {
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
               {
                  //Here I write out the CurrentRowArray information combined with
                  //the PreviousRowArray. Removed for ease of reading
                 
                  PreviousRowArray = null;
                  CurrentRowArray  = null;
               }
               else
               {
        //Here I write only the pervious line out to the
                  //XML file. Removed for ease of reading

                  PreviousRowArray  = CurrentRowArray;
               }
            }
            else//Move currentrow to previous

               PreviousRowArray = CurrentRow.Split(' ');
         }

         //this is the end of the document
         textwriter.WriteEndDocument();              
         textwriter.Flush();
         textwriter.Close();

      }


Avatar of wlfs
wlfs

Hi, I only had a quick look up to now. What catched my eyes is the if condition
               if ((CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
The two sides of the && are identical. I guess it should be
               if ((CurrentRowArray[0].ToString() == PreviousRowArray[0].ToString()) &&
                  (CurrentRowArray[1].ToString() == PreviousRowArray[1].ToString()))
Does this solve your problem?
Avatar of NewMom2Brandon

ASKER

hmm that was a good problem (duh kicking myself) but it still doesn't solve my actual issue.
ASKER CERTIFIED SOLUTION
Avatar of junglerover77
junglerover77

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you!