title: How to read column names of a multicolumn ListView control?
body: <p>What is the best way to find the names of the columns of a ListView?</p>
  
  <p>I converted a DataTable to a List using a procedure I found on this forum, but I cannot make it to put the Id column first, especially because not all of my DataTables have a column "Id".</p>
  
  <p>I can search in collection listView.Columns.ToString() but the format I am seeing is:</p>
  
  <p>"ColumnHeader: Text: Id"</p>
  
  <p>which I have to parse to find the proper name "Id".
  This does not look like the spirit of C#.</p>
  
  <p>I also tried: <code>listView.SelectedItems[0].SubItems["Id"]</code>
  but that does not compile.</p>
  
  <hr>
  
  <p>Ok Here is the complete code.
  The exact problem is that the user selects a row in the listView with Courier Names and Ids, but it could also be Ids and Names, in that order. The fastest way to find the Id of the selected courier would be:</p>
  
  <pre><code>ListViewItem si = listCouriers.SelectedItems[0];
  CourierId = si.SubItems["Id"].Text;
  </code></pre>
  
  <p>but that does not work. The hardcoded way would be this, but I cannot guarantee that some day the wrong column will be used:</p>
  
  <pre><code>ListViewItem si = listCouriers.SelectedItems[0];
  CourierId = si.SubItems[1].Text;
  </code></pre>
  
  <p>Using @HuorSwords method leads to this not-so-simple solution, which works for me, but depends on the reasonable assumption that the order of columns in the <code>ColumnHeaderCollection</code> corresponds to the display on the form:</p>
  
  <pre><code>ListViewItem si = listCouriers.SelectedItems[0];
  string CourierId = null;
  int icol = 0;
  foreach (ColumnHeader header in listCouriers.Columns)
  {
      if (header.Text == "Id")
      {
          CourierId = si.SubItems[icol].Text;
          break;
      }
      icol++;
  }
  </code></pre>