title: Populating a ListView with objects from a List<>
body: <p>I am currently converting what was a Console Application into a Windows Form application.
  Fortunatly i initially designed the application to work with a GUI in the future so there was not much code to change.</p>
  
  <p>I have a UserCollection class which itself holds a List&lt;> of type User/Member (Member is derives from User).</p>
  
  <p>What i would like to do is to add each item in the UserCollection List&lt;> to the ListView, so i can see each entry and have the multiple entries list vertically.</p>
  
  <p>I have tried to implement this myself firstly by using:</p>
  
  <pre><code>        private void UpdatePeopleListings()
      {
          foreach (User person in newCollection)
          {
              listViewPeople.Items.Add(person.ToString());
          }
      }
  </code></pre>
  
  <p>Where newCollection is the new object created from the UserCollection class in the main windows form.</p>
  
  <p>I recieve the error:</p>
  
  <pre><code>foreach statement cannot operate on variables of type 'Collection.UserCollection' because 'Collection.UserCollection' does not contain a public definition for 'GetEnumerator'
  </code></pre>
  
  <p>I then tried to make a small workaround, so in my UserCollection i created the following method:</p>
  
  <pre><code>        public User ReturnUser()
      {
          foreach (User person in _userCollection)
          {
              return person;
          }
          return null;
      }
  </code></pre>
  
  <p>(_userCollection is the List&lt;> of Users/Members in UserCollection.cs)</p>
  
  <p>And then use it like this:</p>
  
  <pre><code>private void UpdatePeopleListings()
      {
          listViewPeople.Items.Add(newCollection.ReturnUser().ToString());
      }
  </code></pre>
  
  <p>Although this does populate the ListView with an entry, it only populates the first entry.
  If i was to add more than one User/Member to the newCollection then it simply repeats the first entry.</p>
  
  <p>How would i go about populating the ListView with ALL objects in the collection properly and how would i prevent it from repeating only one object.</p>
  
  <p>UserCollecton.cs</p>
  
  <pre><code>///////////////////////////////////////////////////////////
  //  UserCollection.cs
  //  Implementation of the Class UserCollection
  //  Generated by Enterprise Architect
  //  Created on:      22-Oct-2009 22:40:30
  ///////////////////////////////////////////////////////////
  
  #region Using Statements
  
  using System;
  
  using System.Collections.Generic;
  
  using System.Runtime.Serialization;
  using System.Runtime.Serialization.Formatters.Binary;
  
  using System.IO;
  
  #endregion
  
  namespace Collection
  {
      //Allows the class to be saved
      [Serializable()]
  
      public class UserCollection
      {
          #region Fields
  
          //Declares a list of type User (Which also holds derived Member objects)
          private List&lt;User&gt; _userCollection = new List&lt;User&gt;();
  
  
          //Holds number of members
          int nMember = 0;
          //Holds number of users
          int nUser = 0;
  
          #endregion
  
          #region Add Operations
  
          /// &lt;summary&gt;
          /// Adds a new user
          /// &lt;/summary&gt;
          /// &lt;param name="user"&gt;&lt;/param&gt;
          public void AddUser(User user)
          {
              //Adds the user given in the operation parameters to the collection
              _userCollection.Add(user);
              nUser++;
  
              //Sorts the collection using the CompareTo() specified in the User class.
              _userCollection.Sort();
  
              //Console.WriteLine used for testing purposes
              //Console.WriteLine("added");
          }
  
  
          ///&lt;summary&gt;
          ///Adds a new Member
          ///&lt;/summary&gt;
          /// &lt;param name="member"&gt;&lt;/param&gt;
          public void AddMember(Member member)
          {
              //Adds the member given in the operation parameters to the collection
              _userCollection.Add(member);
              nMember++;
  
              //Sorts the collection using the CompareTo() specified in the User class.
              _userCollection.Sort();
  
              //Console.WriteLine used for testing purposes
              //Console.WriteLine("added");
          }
  
          #endregion
  
          #region Removal Operations
  
          ///&lt;summary&gt;
          ///Removes a user based on FirstName,LastName and PostCode
          ///&lt;/summary&gt; 
          /// &lt;param name="person"&gt;&lt;/param&gt;
          public void RemoveUser(User person)
          {
              //Only search collection for users if there is data in it
              if (_userCollection.Count &gt; 0)
              {
                  //Create a temp list for any matched found
                  List&lt;User&gt; tempList = new List&lt;User&gt;();
  
                  foreach (User u in _userCollection)
                  {
                      //If the details stored in the collection match the details given in the search
                      if (u.FName == person.FName &amp;&amp; u.LName == person.LName &amp;&amp; u.PostCode == person.PostCode)
                      {
                          //Add any matches to the temp list
                          tempList.Add(u);
                      }
                      else
                      {
                          throw new ArgumentException("User not found");
                      }
                  }
  
                  //Delete any matches 
                  foreach (User u in tempList)
                  {
                      _userCollection.Remove(u);
                      //Decrement user count
                      nUser--;
                  }
              }
              else
              {
                  throw new AccessViolationException("No data in collection");
              }
          }
  
          /// &lt;summary&gt;
          /// Removes a user using Membership number
          /// &lt;/summary&gt;
          /// &lt;param name="number"&gt;&lt;/param&gt;
          public void RemoveMember(int number)
          {
                  //Create a temp list of type Member
                  Member temp = new Member();
  
                  //Use the temp list to compare types and store all objects of type member
                  //found in the collection to it.
                  List&lt;User&gt; Mems = _userCollection.FindAll(delegate(User u)
                  { return u.GetType() == temp.GetType(); });
  
                  //Delete any matches
                  foreach (Member m in Mems)
                  {
                      if (m.mNum == number)
                      {
                          _userCollection.Remove(m);
                          //Decrement member count
                          nMember--;
                      }
                      else
                      {
                          throw new ArgumentException("Member not found");
                      }
                  }
          }
  
          #endregion
  
          #region Search Operations
  
          ///&lt;summary&gt;
          ///Returns member by Membership number
          /// &lt;/summary&gt;
          /// 
          /// &lt;param name="_mNumber"&gt;&lt;/param&gt;
          public Member FindByMNo(int number)
          {
              //Create a temp list of type Member
              Member temp = new Member();
  
              //Use the temp list to compare types and store all objects of type member
              //found in the collection to it.
              List&lt;User&gt; Mems = _userCollection.FindAll(delegate(User u)
              { return u.GetType() == temp.GetType(); });
  
              //Return any matches found
              foreach (Member i in Mems)
              {
                  if (i.mNum == number)
                  {
                      return i;
                  }
              }
              throw new ArgumentException("Member not found");
          }
  
          ///&lt;summary&gt;
          ///Returns a list of Users matching details given
          ///&lt;/summary&gt;
          /// 
          /// &lt;param name="_fName"&gt;&lt;/param&gt;
          /// &lt;param name="_lName"&gt;&lt;/param&gt;
          public List&lt;User&gt; FindByName(User person)
          {
              //Create a temp list to store any matches
              List&lt;User&gt; temp = new List&lt;User&gt;();
  
              //Add matches found to the temp list
              foreach (User u in _userCollection)
              {
                  if (u.LName == person.LName)
                  {
                      temp.Add(u);
                  }
              }
  
              if (temp.Count &gt; 0)
              {
                  //Return the list that holds any matches
                  return temp;
              }
  
              throw new ArgumentException("User not found");
          }
  
          public User ReturnUser()
          {
              foreach (User person in _userCollection)
              {
                  return person;
              }
              return null;
          }
  
          #endregion
  
          #region Edit Operations
  
          ///&lt;summary&gt;
          ///Edits a members membership expiry
          ///&lt;/summary&gt;
          /// 
          /// &lt;param name="member"&gt;&lt;/param&gt;
          public void EditMemStatus(int member, DateTime newDate)
          {
                  //Create a temp list of type Member
                  Member temp = new Member();
  
                  //Use the temp list to compare types and store all objects of type member
                  //found in the collection to it.
                  List&lt;User&gt; Mems = _userCollection.FindAll(delegate(User u)
                  { return u.GetType() == temp.GetType(); });
  
                  //Search for the member that matches the number given in the parameter
                  foreach (Member m in Mems)
                  {
                      if (m.mNum == member)
                      {
                          //Replace the match with the new expiry
                          m.mExp = newDate;
                      }
                      else
                      {
                          throw new ArgumentException("Date cannot be changed");
                      }
                  }
          }
  
          #endregion
  
          #region I/O Operations
  
          public bool SaveData()
          {
              try
              {
                  //Open the stream using the Data.txt file
                  using (Stream stream = File.Open("Data.txt", FileMode.Create))
                  {
                      //Create a new formatter
                      BinaryFormatter bin = new BinaryFormatter();
                      //Copy data in collection to the file specified earlier
                      bin.Serialize(stream, _userCollection);
                      bin.Serialize(stream, nMember);
                      bin.Serialize(stream, nUser);
                      //Close stream to release any resources used
                      stream.Close();
                  }
                  return true;
              }
              catch (IOException ex)
              {
                  throw new ArgumentException(ex.ToString());
              }
          }
  
          public bool LoadData()
          {
              //Check if file exsists, otherwise skip
              if (File.Exists("Data.txt"))
              {
                  try
                  {
                      using (Stream stream = File.Open("Data.txt", FileMode.Open))
                      {
                          BinaryFormatter bin = new BinaryFormatter();
  
                          //Copy data back into collection fields
                          _userCollection = (List&lt;User&gt;)bin.Deserialize(stream);
                          nMember = (int)bin.Deserialize(stream);
                          nUser = (int)bin.Deserialize(stream);
                          stream.Close();
  
                          //Sort data to ensure it is ordered correctly after being loaded
                          _userCollection.Sort();
                          return true;
  
                      }
                  }
                  catch (IOException ex)
                  {
                      throw new ArgumentException(ex.ToString());
                  }
              }
              else
              {
                  //Console.WriteLine present for testing purposes
                  Console.WriteLine("\nLoad failed, Data.txt not found");
                  return false;
              }
          }
  
          #endregion
  
          #region Properties
  
          //Gets amount of Members in collection
          public int GetNMember
          {
              get
              {
                  return nMember;
              }
          }
  
          //Gets amount of Users in collectioj
          public int GetNUser
          {
              get
              {
                  return nUser;
              }
          }
  
          #endregion
  
      }//end UserCollection
  }
  </code></pre>
  
  <p>Ignore any random Console stuff, i've not finished the cleanup yet.</p>