title: How to keep multiple listviews' items selected in a Win forms app
body: <p>I have what I hope to be an easy solution for you fine, intelligent folks. I have a Windows form app (C#) and it has 2 listviews. The user will choose at least one item from listview1 (we will call it people) and at least one item from listview2 (we will call it offices). The point is to assign people to offices and save that in a DB.</p>
  
  <p>The issue is that once they select an item(s) from one listview and click anything on the other listview, it clears the displayed choices from the initial listview. I know I can grab the info programmatically but the user is going to think that their choices from the first listview were erased. Any ideas on how to keep the selections on the first listview visible?</p>
  
  <p>Update: Here is a screenshot of the app so far: (it says I need 1`0 rep before I can post an image so I will just link to it)
  <a href="http://s17.postimg.org/wcajg7o4v/Untitled.png" rel="nofollow">http://s17.postimg.org/wcajg7o4v/Untitled.png</a></p>
  
  <p>The user clicks an office and then clicks a case(s) and I make an entry in the db to persist their IDs. The issue is that if I click an office and then do anything in the other listview (Cases) then the items I selected in Offices are no longer visible. The code is over 1500 lines and counting so I am unsure exactly what code you need. The only manipulation I am using of the listviews is a sorting method and populating them of course. Here is that code (or some of it). The loadOfficeList method populates the Offices listview and the class below it is my sorting class for all listviews (with their own respective objects, of course).</p>
  
  <pre><code>        private void loadOfficeList()
      {
          listViewOffices.Clear();
          listViewOffices.Items.Clear();
          DataTable t = new DataTable();
          using (SqlConnection c = new SqlConnection(connectionString))
          {
              c.Open();
              // 2
              // Create new DataAdapter
              using (SqlDataAdapter a = new SqlDataAdapter("select ID, Name from Offices where deleted = 'false'", c))
              {
                  DataSet ds = new DataSet();
                  a.Fill(ds);
                  t = ds.Tables[0];
              }//end inner using
          }//end outer using
  
          // Set DataSource property of ListBox as DataSet's DefaultView
  
          this.listViewOffices.TabIndex = 0;
          this.listViewOffices.View = System.Windows.Forms.View.List;
          ColumnHeader colHead;
  
          colHead = new ColumnHeader();
          colHead.Width = 250;
          colHead.Text = "Office Name";
          this.listViewOffices.Columns.Add(colHead);
  
  
          colHead = new ColumnHeader();
          colHead.Width = 40;
          colHead.Text = "ID";
          this.listViewOffices.Columns.Add(colHead);
  
          ListViewItem lvi;
          ListViewItem.ListViewSubItem lvsi;
  
          foreach (DataRow dataRow in t.Rows)
          {
  
              lvi = new ListViewItem();
              lvi.Text = dataRow["Name"].ToString();
              lvi.Tag = dataRow["Name"].ToString();
  
              lvsi = new ListViewItem.ListViewSubItem();
              lvsi.Text = dataRow["ID"].ToString();
              lvi.SubItems.Add(lvsi);
  
  
              this.listViewOffices.Items.Add(lvi);
              this.listViewOffices.EndUpdate();
  
              this.listViewOffices.View = System.Windows.Forms.View.Details;
  
          }//end foreach
  
      }//end method
  
  public class ListViewColumnSorter : IComparer
  {
      // Specifies the column to be sorted
      private int ColumnToSort;
  
      // Specifies the order in which to sort (i.e. 'Ascending').
      private SortOrder OrderOfSort;
  
      // Case insensitive comparer object
      private CaseInsensitiveComparer ObjectCompare;
  
  
      // Class constructor.  Initializes various elements
      public ListViewColumnSorter()
      {
          // Initialize the column to '0'
          ColumnToSort = 0;
  
          // Initialize the sort order to 'none'
          OrderOfSort = SortOrder.None;
  
          // Initialize the CaseInsensitiveComparer object
          ObjectCompare = new CaseInsensitiveComparer();
      }//end constructor
  
  
      // This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
      // &lt;returns&gt;The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'&lt;/returns&gt;
      public int Compare(object x, object y)
      {
          int compareResult;
          ListViewItem listviewX, listviewY;
  
          // Cast the objects to be compared to ListViewItem objects
          listviewX = (ListViewItem)x;
          listviewY = (ListViewItem)y;
  
          compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
  
          // Calculate correct return value based on object comparison
          if (OrderOfSort == SortOrder.Ascending)
              return compareResult;
          else if (OrderOfSort == SortOrder.Descending)
              return (-compareResult);
          else
              return 0;
      }//end method
  
      // Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
      public int SortColumn
      {
          set
          { ColumnToSort = value; }
          get
          { return ColumnToSort; }
      }//end method
  
      // Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
      public SortOrder Order
      {
          set
          { OrderOfSort = value; }
          get
          { return OrderOfSort; }
      }//end method
  
  }//end class
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>Dan</p>