title: Add columns in listview in c#.net
body: <p>Can you tell me how to add a column in listview</p>
  
  <p>i have a list view named "_listAvailable",</p>
  
  <p>i need to add a column named "Name" but i dont know how to do it.this is a simple way 
  ColumnHeader columnHeader1 = new ColumnHeader();
  columnHeader1.Text = "Name";
  listView1.Columns.Add(columnHeader1);
  but our project they are doing everything differently i wl give my coding page
  here for example "Resource Policy" is an example of one column header in listview("_listAvailable")  its name is "_chPolicy"(variable used in coding side) .Its text is taking from resource files (.resx) files</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Windows.Forms;
  using System.Reflection;
  
  using leaf.Utilities;
  using leaf.Plugins;
  using leaf.DataModel;
  using leaf.DataModel.Attributes;
  using leaf.DataModel.Exceptions;
  using leaf.Controls.Resources;
  using leaf.Controls.Dialogs;
  using leafControl = leaf.Controls.Controls;
  
  namespace leaf.Controls
  {
      /// &lt;summary&gt;
      /// Editor displayed in the details pane for associating host systems with a resource policy.
      /// &lt;/summary&gt;
      public class ResourcePolicySystemsControl : System.Windows.Forms.UserControl
      {
          #region ResourcePolicySystemsLVI
  
          /// &lt;summary&gt;
          /// ListViewItem class for displaying an item in the listview of 
          /// the ResourcePolicySystemsControl.
          /// &lt;/summary&gt;
          public class ResourcePolicySystemsLVI : BaseLVItem
          {
              #region Properties
  
              /// &lt;summary&gt;
              /// The IResourcePolicy interface of the ManagedDevice associated with this ListViewItem.
              /// &lt;/summary&gt;
              public new IResourcePolicy Data
              {
                  get
                  {
                      return (IResourcePolicy)base.Data;
                  }
              }
  
              #endregion Properties
  
              #region Constructors and Finalizers
  
              /// &lt;summary&gt;
              /// Constructor for a ResourcePolicySystemsLVI.
              /// &lt;/summary&gt;
              /// &lt;param name="md"&gt;Managed Device item
              /// being displayed.&lt;/param&gt;
              public ResourcePolicySystemsLVI(ManagedDevice md)
                  : base(md)
              {
              }
  
              #endregion Constructors and Finalizers
  
              #region Methods
  
              /// &lt;summary&gt;
              /// Initially create and update this item and subitems.
              /// &lt;/summary&gt;
              protected override void OnUpdate()
              {
                  string func = "ResourcePolicySystemsLVI.OnUpdate";
                  try
                  {
                      if (Data != null)
                      {
                          Text = base.Data.Name;
                          if (SubItems.Count == 1)
                          {
                              SubItems.Add(((IResourcePolicy)Data).ResourcePolicyEnabled.ToString());
                              SubItems.Add(((IResourcePolicy)Data).ResourcePolicyCurrent.ToString());
                          }
                          else
                          {
                              SubItems[1].Text = ((IResourcePolicy)Data).ResourcePolicyEnabled.ToString();
                              SubItems[2].Text = ((IResourcePolicy)Data).ResourcePolicyCurrent.ToString();
                          }
                      }
                      base.OnUpdate();
                  }
                  catch (System.Exception exception)
                  {
                      leafDebug.DebugTraceSevere(func, "Exception: " + exception.ToString());
                  }
              }
  
              #endregion Methods
          }
  
          #endregion
  
          #region ResourcePolicyAvailSystemsLVI
  
          /// &lt;summary&gt;
          /// ListViewItem class for displaying an item in the listview of 
          /// the ResourcePolicySystemsControl.
          /// &lt;/summary&gt;
          public class ResourcePolicyAvailSystemsLVI : BaseLVItem
          {
              #region Fields
  
              private static string m_nullString = Managers.ControlStrings.GetString("ManagedDeviceWizard.None");
  
              #endregion Fields
  
              #region Properties
  
              /// &lt;summary&gt;
              /// The IResourcePolicy interface of the ManagedDevice associated with this ListViewItem.
              /// &lt;/summary&gt;
              public new IResourcePolicy Data
              {
                  get
                  {
                      return (IResourcePolicy)base.Data;
                  }
              }
  
              #endregion Properties
  
              #region Constructors and Finalizers
  
              /// &lt;summary&gt;
              /// Constructor for a ResourcePolicyAvailSystemsLVI.
              /// &lt;/summary&gt;
              /// &lt;param name="md"&gt;Managed Device item
              /// being displayed.&lt;/param&gt;
              public ResourcePolicyAvailSystemsLVI(ManagedDevice md)
                  : base(md)
              {
              }
  
              #endregion Constructors and Finalizers
  
              #region Methods
  
              /// &lt;summary&gt;
              /// Initially create and update this item and subitems.
              /// &lt;/summary&gt;
              protected override void OnUpdate()
              {
                  string func = "ResourcePolicyAvailSystemsLVI.OnUpdate";
                  try
                  {
                      if (Data != null)
                      {
                          Text = base.Data.Name;
                          if (SubItems.Count == 1)
                          {
                              if (Data.ResourcePolicy == null)
                                  SubItems.Add(ResourcePolicyAvailSystemsLVI.m_nullString);
                              else
                                  SubItems.Add(Data.ResourcePolicy.Name);
                          }
                          else
                          {
                              if (Data.ResourcePolicy == null)
                                  SubItems[1].Text = ResourcePolicyAvailSystemsLVI.m_nullString;
                              else
                                  SubItems[1].Text = Data.ResourcePolicy.Name;
                          }
                      }
                      base.OnUpdate();
                  }
                  catch (System.Exception exception)
                  {
                      leafDebug.DebugTraceSevere(func, "Exception: " + exception.ToString());
                  }
              }
  
              #endregion Methods
          }
  
          #endregion
  
          #region Fields
  
          private IContainer components;
  
          // Indicates a refresh is necessary.
          private bool _refresh = false;
  
          // leaf server reference.
          private leaf.Plugins.leafServer _leafServer = null;
  
          // Gui components.
          private Panel _panelBorder;
          private ContextMenuStrip _cmenu;
          private ToolStripMenuItem _cmenuDistribute;
          private ToolStripMenuItem _cmenuEnable;
          private ToolStripMenuItem _cmenuDisable;
          private SplitContainer _splitter;
          private Label _lblSelected;
          private leafListView _listAvailable;
          private ColumnHeader _chNameAvailable;
          private Label _lblAvailable;
          private leafListView _listSelected;
          private ColumnHeader _chNameSelected;
          private ColumnHeader _chEnabledSelected;
          private ColumnHeader _chCurrentSelected;
  
          #endregion Fields
  
          #region Events
  
          /// &lt;summary&gt;
          /// Event raised when the SelectedObjects have changed.
          /// &lt;/summary&gt;
          public event System.EventHandler SelectedObjectsChanged;
  
          #endregion Events
  
          #region Properties
  
          /// &lt;summary&gt;
          /// Get the ListView control.
          /// &lt;/summary&gt;
          public leafListView ListView
          {
              get
              {
                  return _listSelected;
              }
          }
  
          /// &lt;summary&gt;
          /// Get/set the selected objects for members display.
          /// &lt;/summary&gt;
          public object[] SelectedObjects
          {
              get
              {
                  return m_selectedObjects;
              }
              set
              {
                  OnSelectedObjectsChanging();
                  m_selectedObjects = value;
                  OnSelectedObjectsChanged();
              }
          }
          private object[] m_selectedObjects = null;
  
          /// &lt;summary&gt;
          /// Selected resource policy the host systems are to be assigned with.
          /// &lt;/summary&gt;
          public ResourcePolicy ResourcePolicy
          {
              get
              {
                  return m_resourcePolicy;
              }
              set
              {
                  if (m_resourcePolicy != null)
                  {
                      m_resourcePolicy.Updated -= new EventHandler(m_resourcePolicy_Updated);
                  }
                  m_resourcePolicy = value;
                  if (m_resourcePolicy != null)
                  {
                      m_resourcePolicy.Updated += new EventHandler(m_resourcePolicy_Updated);
                  }
              }
          }
          private ResourcePolicy m_resourcePolicy = null;
          private ColumnHeader _chPolicy;
  
          /// &lt;summary&gt;
          /// Get/set the leaf Server object for the connection.
          /// &lt;/summary&gt;
          public leaf.Plugins.leafServer leafServer
          {
              get
              {
                  return _leafServer;
              }
              set
              {
                  if (_leafServer != value)
                  {
                      if (_leafServer != null)
                      {
                          _leafServer.ObjectReceived -= new leafServer.ObjectEventHandler(ObjectReceived);
                      }
                      _leafServer = value;
                      if (_leafServer != null)
                      {
                          _leafServer.ObjectReceived += new leafServer.ObjectEventHandler(ObjectReceived);
                      }
                      _refresh = true;
                  }
              }
          }
  
          /// &lt;summary&gt;
          /// Get the list of host systems that is currently associated with
          /// the selected resource policy for an OS type.
          /// &lt;/summary&gt;
          public DataModelCollection PolicySystemsList
          {
              get
              {
                  if (ResourcePolicy == null)
                      return new ManagedDeviceCollection();
                  else
                      return ResourcePolicy.ManagedDevices;
              }
          }
  
          /// &lt;summary&gt;
          /// Get the list of host systems that is currently NOT associated with
          /// the current resource policy.
          /// &lt;/summary&gt;
          public DataModelCollection AvailableSystemsList
          {
              get
              {
                  ManagedDeviceCollection mdc = new ManagedDeviceCollection();
                  ObjectArray coll;
  
                  if (ResourcePolicy == null)
                      return mdc;
  
                  if (ResourcePolicy is WindowsResourcePolicy)
                  {
                      coll = leafServer.GetObjectsByType(typeof(WindowsSystem));
                  }
                  else
                  {
                      coll = leafServer.GetObjectsByType(typeof(UnixSystem));
                  }
                  foreach (ManagedDevice dev in coll)
                  {
                      if (dev.ImmediateState != ModifiedState.ToBeDeleted &amp;&amp;
                         ((IResourcePolicy)dev).ResourcePolicy != ResourcePolicy)
                          mdc.Add(dev);
                  }
  
                  return mdc;
              }
          }
  
          /// &lt;summary&gt;
          /// the resource policy OS node associated with the control.
          /// &lt;/summary&gt;
          public ResourcePolicyOSNode Node
          {
              get
              {
                  return m_node;
              }
              set
              {
                  m_node = value;
              }
          }
          private ResourcePolicyOSNode m_node = null;
  
          /// &lt;summary&gt;
          /// Indicates if an Enable can be performed.
          /// &lt;/summary&gt;
          public bool CanEnable
          {
              get
              {
                  foreach (ResourcePolicySystemsLVI lvi in _listSelected.SelectedItems)
                  {
                      if (!lvi.Data.ResourcePolicyEnabled)
                      {
                          return true;
                      }
                  }
                  return false;
              }
          }
  
          /// &lt;summary&gt;
          /// Indicates if a Disable can be performed.
          /// &lt;/summary&gt;
          public bool CanDisable
          {
              get
              {
                  foreach (ResourcePolicySystemsLVI lvi in _listSelected.SelectedItems)
                  {
                      if (lvi.Data.ResourcePolicyEnabled)
                      {
                          return true;
                      }
                  }
  
                  return false;
              }
          }
  
          /// &lt;summary&gt;
          /// Indicates if a Distribute operation can be performed.
          /// &lt;/summary&gt;
          public bool CanDistribute
          {
              get
              {
                  return this._listSelected.SelectedItems.Count &gt; 0 ||
                     this._listAvailable.SelectedItems.Count &gt; 0;
              }
          }
  
          #endregion Properties
  
          #region Constructors and Finalizers
  
          /// &lt;summary&gt;
          /// Create a ResourcePolicySystemsControl with default settings.  
          /// Must set the ResourcePolicy object before display.
          /// &lt;/summary&gt;
          public ResourcePolicySystemsControl()
          {
              // This call is required by the Windows.Forms Form Designer.
              InitializeComponent();
  
              // Attach to application idle
              Application.Idle += new EventHandler(Application_Idle);
          }
  
          /// &lt;summary&gt; 
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          protected override void Dileafse(bool dileafsing)
          {
              // Remove selected objects.
              m_selectedObjects = null;
  
              // Remove application idle handler.
              Application.Idle -= new EventHandler(Application_Idle);
  
              // Remove object received handler.
              _leafServer.ObjectReceived -= new leafServer.ObjectEventHandler(ObjectReceived);
  
              // Remove resource policy update handler
              if (m_resourcePolicy != null)
              {
                  m_resourcePolicy.Updated -= new EventHandler(m_resourcePolicy_Updated);
              }
  
              if (dileafsing)
              {
                  if (components != null)
                  {
                      components.Dileafse();
                  }
              }
              base.Dileafse(dileafsing);
          }
  
          #endregion Constructors and Finalizers
  
          #region Component Designer generated code
          /// &lt;summary&gt; 
          /// Required method for Designer support - do not modify 
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {
              this.components = new System.ComponentModel.Container();
              System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ResourcePolicySystemsControl));
              this._panelBorder = new System.Windows.Forms.Panel();
              this._splitter = new System.Windows.Forms.SplitContainer();
              this._listSelected = new leaf.Plugins.leafListView();
              this._chNameSelected = new System.Windows.Forms.ColumnHeader();
              this._chEnabledSelected = new System.Windows.Forms.ColumnHeader();
              this._chCurrentSelected = new System.Windows.Forms.ColumnHeader();
              this._cmenu = new System.Windows.Forms.ContextMenuStrip(this.components);
              this._cmenuEnable = new System.Windows.Forms.ToolStripMenuItem();
              this._cmenuDisable = new System.Windows.Forms.ToolStripMenuItem();
              this._cmenuDistribute = new System.Windows.Forms.ToolStripMenuItem();
              this._lblSelected = new System.Windows.Forms.Label();
              this._listAvailable = new leaf.Plugins.leafListView();
              this._chNameAvailable = new System.Windows.Forms.ColumnHeader();
              this._chPolicy = new System.Windows.Forms.ColumnHeader();
              this._lblAvailable = new System.Windows.Forms.Label();
              this._panelBorder.SuspendLayout();
              this._splitter.Panel1.SuspendLayout();
              this._splitter.Panel2.SuspendLayout();
              this._splitter.SuspendLayout();
              this._cmenu.SuspendLayout();
              this.SuspendLayout();
              // 
              // _panelBorder
              // 
              this._panelBorder.Controls.Add(this._splitter);
              resources.ApplyResources(this._panelBorder, "_panelBorder");
              this._panelBorder.Name = "_panelBorder";
              // 
              // _splitter
              // 
              resources.ApplyResources(this._splitter, "_splitter");
              this._splitter.Name = "_splitter";
              // 
              // _splitter.Panel1
              // 
              this._splitter.Panel1.Controls.Add(this._listSelected);
              this._splitter.Panel1.Controls.Add(this._lblSelected);
              // 
              // _splitter.Panel2
              // 
              this._splitter.Panel2.Controls.Add(this._listAvailable);
              this._splitter.Panel2.Controls.Add(this._lblAvailable);
              // 
              // _listSelected
              // 
              this._listSelected.AllowColumnReorder = true;
              this._listSelected.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
              this._chNameSelected,
              this._chEnabledSelected,
              this._chCurrentSelected});
              this._listSelected.ContextMenuStrip = this._cmenu;
              resources.ApplyResources(this._listSelected, "_listSelected");
              this._listSelected.FullRowSelect = true;
              this._listSelected.HideSelection = false;
              this._listSelected.Name = "_listSelected";
              this._listSelected.ShowItemToolTips = true;
              this._listSelected.UseCompatibleStateImageBehavior = false;
              this._listSelected.View = System.Windows.Forms.View.Details;
              this._listSelected.Enter += new System.EventHandler(this.List_Enter);
              this._listSelected.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.List_ColumnClick);
              // 
              // _chNameSelected
              // 
              resources.ApplyResources(this._chNameSelected, "_chNameSelected");
              // 
              // _chEnabledSelected
              // 
              resources.ApplyResources(this._chEnabledSelected, "_chEnabledSelected");
              // 
              // _chCurrentSelected
              // 
              resources.ApplyResources(this._chCurrentSelected, "_chCurrentSelected");
              // 
              // _cmenu
              // 
              this._cmenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
              this._cmenuEnable,
              this._cmenuDisable,
              this._cmenuDistribute});
              this._cmenu.Name = "_contextMenuStrip";
              resources.ApplyResources(this._cmenu, "_cmenu");
              this._cmenu.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip_Opening);
              // 
              // _cmenuEnable
              // 
              this._cmenuEnable.Name = "_cmenuEnable";
              resources.ApplyResources(this._cmenuEnable, "_cmenuEnable");
              this._cmenuEnable.Click += new System.EventHandler(this.EnableClick);
              // 
              // _cmenuDisable
              // 
              this._cmenuDisable.Name = "_cmenuDisable";
              resources.ApplyResources(this._cmenuDisable, "_cmenuDisable");
              this._cmenuDisable.Click += new System.EventHandler(this.DisableClick);
              // 
              // _cmenuDistribute
              // 
              this._cmenuDistribute.Name = "_cmenuDistribute";
              resources.ApplyResources(this._cmenuDistribute, "_cmenuDistribute");
              this._cmenuDistribute.Click += new System.EventHandler(this.DistributeClick);
              // 
              // _lblSelected
              // 
              resources.ApplyResources(this._lblSelected, "_lblSelected");
              this._lblSelected.Name = "_lblSelected";
              // 
              // _listAvailable
              // 
              this._listAvailable.AllowColumnReorder = true;
              this._listAvailable.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
              this._chNameAvailable,
              this._chPolicy});
              this._listAvailable.ContextMenuStrip = this._cmenu;
              resources.ApplyResources(this._listAvailable, "_listAvailable");
              this._listAvailable.FullRowSelect = true;
              this._listAvailable.HideSelection = false;
              this._listAvailable.Name = "_listAvailable";
              this._listAvailable.ShowItemToolTips = true;
              this._listAvailable.UseCompatibleStateImageBehavior = false;
              this._listAvailable.View = System.Windows.Forms.View.Details;
              this._listAvailable.Enter += new System.EventHandler(this.List_Enter);
              this._listAvailable.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.List_ColumnClick);
              // 
              // _chNameAvailable
              // 
              resources.ApplyResources(this._chNameAvailable, "_chNameAvailable");
              // 
              // _chPolicy
              // 
              resources.ApplyResources(this._chPolicy, "_chPolicy");
              // 
              // _lblAvailable
              // 
              resources.ApplyResources(this._lblAvailable, "_lblAvailable");
              this._lblAvailable.Name = "_lblAvailable";
              // 
              // ResourcePolicySystemsControl
              // 
              this.Controls.Add(this._panelBorder);
              this.Name = "ResourcePolicySystemsControl";
              resources.ApplyResources(this, "$this");
              this.Load += new System.EventHandler(this.ResourePolicySystemsControl_Load);
              this._panelBorder.ResumeLayout(false);
              this._splitter.Panel1.ResumeLayout(false);
              this._splitter.Panel2.ResumeLayout(false);
              this._splitter.ResumeLayout(false);
              this._cmenu.ResumeLayout(false);
              this.ResumeLayout(false);
  
          }
          #endregion
  
          #region Methods
  
          /// &lt;summary&gt;
          /// Removes the update handler from the selected objects.
          /// &lt;/summary&gt;
          private void OnSelectedObjectsChanging()
          {
              if (m_selectedObjects != null)
              {
                  foreach (ResourcePolicy rp in m_selectedObjects)
                  {
                      rp.Updated -= new EventHandler(DeviceUpdated);
                      foreach (ManagedDevice md in rp.ManagedDevices)
                          md.Updated -= new EventHandler(DeviceUpdated);
                  }
              }
          }
  
          /// &lt;summary&gt;
          /// Fills in the list view control with the appropriate managed
          /// device objects and button display, and raises the SelectedObjectsChanged event.
          /// &lt;/summary&gt;
          private void OnSelectedObjectsChanged()
          {
              _refresh = true;
              if (m_selectedObjects != null)
              {
                  foreach (ResourcePolicy rp in m_selectedObjects)
                  {
                      rp.Updated += new EventHandler(DeviceUpdated);
                      foreach (ManagedDevice md in rp.ManagedDevices)
                          md.Updated += new EventHandler(DeviceUpdated);
                  }
              }
              if (SelectedObjectsChanged != null)
              {
                  SelectedObjectsChanged(this, new System.EventArgs());
              }
          }
  
          /// &lt;summary&gt;
          /// Populate the available list and the selected list.
          /// &lt;/summary&gt;
          public void FillOutListViewCtrl()
          {
              string func = "ResourcePolicySystemsControl.FillOutListViewCtrl";
              ManagedDeviceCollection list = new ManagedDeviceCollection();
  
              try
              {
                  if (leafServer == null)
                      return;
  
                  _listSelected.BeginUpdate();
                  _listAvailable.BeginUpdate();
  
                  #region populate the selected list
  
                  // Collect selected items.
                  object[] selected = new object[_listSelected.SelectedItems.Count];
                  _listSelected.SelectedItems.CopyTo(selected, 0);
  
                  // Clear listview.
                  _listSelected.Items.Clear();
  
                  // Add systems.
                  ResourcePolicySystemsLVI item;
                  foreach (ManagedDevice md in PolicySystemsList)
                  {
                      item = new ResourcePolicySystemsLVI(md);
                      item.Update();
                      foreach (object obj in selected)
                      {
                          item.Selected = (((ResourcePolicySystemsLVI)obj).Data == item.Data);
                          break;
                      }
                      _listSelected.Items.Add(item);
                  }
  
                  // Sort list.
                  _listSelected.Sort();
  
                  // Ensure selected items are visible.
                  if (_listSelected.SelectedItems.Count &gt; 0)
                  {
                      _listSelected.SelectedItems[_listSelected.SelectedItems.Count - 1].EnsureVisible();
                      _listSelected.SelectedItems[0].EnsureVisible();
                  }
  
                  #endregion
  
                  #region populate the available list
  
                  // Collect selected items.
                  selected = new object[_listAvailable.SelectedItems.Count];
                  _listAvailable.SelectedItems.CopyTo(selected, 0);
  
                  // Clear listview.
                  _listAvailable.Items.Clear();
  
                  // Add systems.
                  ResourcePolicyAvailSystemsLVI item2;
                  foreach (ManagedDevice md in AvailableSystemsList)
                  {
                      item2 = new ResourcePolicyAvailSystemsLVI(md);
  </code></pre>