title: How to remove columns from ListView c#
body: <p>I'm new with <code>ListView</code> and encountered a small problem :</p>
  
  <p>I have a <code>ListView</code>in <code>WinForm</code> which I fill it line after line with many columns (about 15) , later on I have an option to save <code>ListView</code> to <code>CSV</code> . 
  what I want is a way to let the user delete unwanted columns from <code>ListView</code> .</p>
  
  <p>My function to create the columns at first </p>
  
  <pre><code>    private void checkAddListBoxColumns(DomainLayer.ScriptLogic.session sessionToAdd)
      {
          if (IsResultLogListViewFirstRun)
          {
              IsResultLogListViewFirstRun = false;
              this.Invoke((MethodInvoker)delegate
              {
                  ResultsLogTab_ListView.Columns.Clear();
              });
  
              foreach (var item in sessionToAdd.comments.Keys)
              {
                  this.Invoke((MethodInvoker)delegate
                  {
                      ResultsLogTab_ListView.Columns.Add(item);
                  });
              }
              McsTableRow mcsTR = new McsTableRow();
              Type t = mcsTR.GetType();
              foreach (/*PropertyInfo*/FieldInfo info in t.GetFields())
              {
                  this.Invoke((MethodInvoker)delegate
                  {
                      ResultsLogTab_ListView.Columns.Add(info.Name+" 1");
                  });
              }
              foreach (FieldInfo info in t.GetFields())
              {
                  this.Invoke((MethodInvoker)delegate
                  {
                      ResultsLogTab_ListView.Columns.Add(info.Name+" 2");
                  });
              }
              this.Invoke((MethodInvoker)delegate
              {
                  ResultsLogTab_ListView.Columns.Add("Effective PHY Rate");
              });
              //...more of the same
          }
          else
          {
              //?
          }
      }
  </code></pre>
  
  <p>later I add line like </p>
  
  <pre><code>            private OnSessionToAdd()
              {
              foreach (string key in sessionToAdd.comments.Keys)
              {
                  item.SubItems.Add(sessionToAdd.comments[key]);
              }
  
              Type t = sessionToAdd.CurrentMCSCheck.firstMcsRow.GetType();
  
              foreach (FieldInfo info in t.GetFields())
              {
                  string x = info.GetValue(sessionToAdd.CurrentMCSCheck.firstMcsRow) as string;
                  item.SubItems.Add(x);
              }
  
              t = sessionToAdd.CurrentMCSCheck.secondMcsRow.GetType();
  
              foreach (FieldInfo info in t.GetFields())
              {
                  string x = info.GetValue(sessionToAdd.CurrentMCSCheck.secondMcsRow) as string;
                  item.SubItems.Add(x);
              }
  
              item.SubItems.Add(sessionToAdd.CurrentMCSCheck.EffectivePHYRate);
  
              this.Invoke((MethodInvoker)delegate
              {
               ResultsLogTab_ListView.Items.Add(item);
              });
              }
  </code></pre>
  
  <p>What i need is a way to get all names of columns and than delete columns by name or some kind of IndexOf(name) </p>
  
  <p>please point me to the right direction</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>just to clearfy I dont to hide columns (settings width = 0) I want to delete them 
  to help you understand my point I've added the way I save the <code>ListView</code>
  the way I save to CSV :</p>
  
  <pre><code>public StringBuilder ListViewToCSV(ListView listView, string filePath, bool includeHidden)
  {
      //make header string
      StringBuilder result = new StringBuilder();
      WriteCSVRow(result, listView.Columns.Count, i =&gt; includeHidden || listView.Columns[i].Width &gt; 0, i =&gt; listView.Columns[i].Text);
  
      //export data rows
      foreach (ListViewItem listItem in listView.Items)
          WriteCSVRow(result, listView.Columns.Count, i =&gt; includeHidden || listView.Columns[i].Width &gt; 0, i =&gt; listItem.SubItems[i].Text);
  
      return result;
  
  }
  
  private static void WriteCSVRow(StringBuilder result, int itemsCount, Func&lt;int, bool&gt; isColumnNeeded, Func&lt;int, string&gt; columnValue)
  {
      bool isFirstTime = true;
      for (int i = 0; i &lt; itemsCount; i++)
      {
          if (!isColumnNeeded(i))
              continue;
  
          if (!isFirstTime)
              result.Append(",");
          isFirstTime = false;
  
          result.Append(String.Format("\"{0}\"", columnValue(i)));
      }
      result.AppendLine();
  }
  </code></pre>
  
  <p>than just save <code>StringBuilder</code></p>