在c#.net中的listview中添加列

时间:2009-09-22 11:47:21

标签: c# listview

您能告诉我如何在listview中添加列

我有一个名为“_listAvailable”的列表视图,

我需要添加一个名为“Name”的列,但我不知道该怎么做。这是一种简单的方法 ColumnHeader columnHeader1 = new ColumnHeader(); columnHeader1.Text =“名称”; listView1.Columns.Add(columnHeader1); 但我们的项目他们做的一切都不同我会给我的编码页面 这里例如“资源策略”是listview中一个列标题的示例(“_ listAvailable”),其名称是“_chPolicy”(编码方使用的变量)。它的文本来自资源文件(.resx)文件

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
{
    /// <summary>
    /// Editor displayed in the details pane for associating host systems with a resource policy.
    /// </summary>
    public class ResourcePolicySystemsControl : System.Windows.Forms.UserControl
    {
        #region ResourcePolicySystemsLVI

        /// <summary>
        /// ListViewItem class for displaying an item in the listview of 
        /// the ResourcePolicySystemsControl.
        /// </summary>
        public class ResourcePolicySystemsLVI : BaseLVItem
        {
            #region Properties

            /// <summary>
            /// The IResourcePolicy interface of the ManagedDevice associated with this ListViewItem.
            /// </summary>
            public new IResourcePolicy Data
            {
                get
                {
                    return (IResourcePolicy)base.Data;
                }
            }

            #endregion Properties

            #region Constructors and Finalizers

            /// <summary>
            /// Constructor for a ResourcePolicySystemsLVI.
            /// </summary>
            /// <param name="md">Managed Device item
            /// being displayed.</param>
            public ResourcePolicySystemsLVI(ManagedDevice md)
                : base(md)
            {
            }

            #endregion Constructors and Finalizers

            #region Methods

            /// <summary>
            /// Initially create and update this item and subitems.
            /// </summary>
            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

        /// <summary>
        /// ListViewItem class for displaying an item in the listview of 
        /// the ResourcePolicySystemsControl.
        /// </summary>
        public class ResourcePolicyAvailSystemsLVI : BaseLVItem
        {
            #region Fields

            private static string m_nullString = Managers.ControlStrings.GetString("ManagedDeviceWizard.None");

            #endregion Fields

            #region Properties

            /// <summary>
            /// The IResourcePolicy interface of the ManagedDevice associated with this ListViewItem.
            /// </summary>
            public new IResourcePolicy Data
            {
                get
                {
                    return (IResourcePolicy)base.Data;
                }
            }

            #endregion Properties

            #region Constructors and Finalizers

            /// <summary>
            /// Constructor for a ResourcePolicyAvailSystemsLVI.
            /// </summary>
            /// <param name="md">Managed Device item
            /// being displayed.</param>
            public ResourcePolicyAvailSystemsLVI(ManagedDevice md)
                : base(md)
            {
            }

            #endregion Constructors and Finalizers

            #region Methods

            /// <summary>
            /// Initially create and update this item and subitems.
            /// </summary>
            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

        /// <summary>
        /// Event raised when the SelectedObjects have changed.
        /// </summary>
        public event System.EventHandler SelectedObjectsChanged;

        #endregion Events

        #region Properties

        /// <summary>
        /// Get the ListView control.
        /// </summary>
        public leafListView ListView
        {
            get
            {
                return _listSelected;
            }
        }

        /// <summary>
        /// Get/set the selected objects for members display.
        /// </summary>
        public object[] SelectedObjects
        {
            get
            {
                return m_selectedObjects;
            }
            set
            {
                OnSelectedObjectsChanging();
                m_selectedObjects = value;
                OnSelectedObjectsChanged();
            }
        }
        private object[] m_selectedObjects = null;

        /// <summary>
        /// Selected resource policy the host systems are to be assigned with.
        /// </summary>
        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;

        /// <summary>
        /// Get/set the leaf Server object for the connection.
        /// </summary>
        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;
                }
            }
        }

        /// <summary>
        /// Get the list of host systems that is currently associated with
        /// the selected resource policy for an OS type.
        /// </summary>
        public DataModelCollection PolicySystemsList
        {
            get
            {
                if (ResourcePolicy == null)
                    return new ManagedDeviceCollection();
                else
                    return ResourcePolicy.ManagedDevices;
            }
        }

        /// <summary>
        /// Get the list of host systems that is currently NOT associated with
        /// the current resource policy.
        /// </summary>
        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 &&
                       ((IResourcePolicy)dev).ResourcePolicy != ResourcePolicy)
                        mdc.Add(dev);
                }

                return mdc;
            }
        }

        /// <summary>
        /// the resource policy OS node associated with the control.
        /// </summary>
        public ResourcePolicyOSNode Node
        {
            get
            {
                return m_node;
            }
            set
            {
                m_node = value;
            }
        }
        private ResourcePolicyOSNode m_node = null;

        /// <summary>
        /// Indicates if an Enable can be performed.
        /// </summary>
        public bool CanEnable
        {
            get
            {
                foreach (ResourcePolicySystemsLVI lvi in _listSelected.SelectedItems)
                {
                    if (!lvi.Data.ResourcePolicyEnabled)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// Indicates if a Disable can be performed.
        /// </summary>
        public bool CanDisable
        {
            get
            {
                foreach (ResourcePolicySystemsLVI lvi in _listSelected.SelectedItems)
                {
                    if (lvi.Data.ResourcePolicyEnabled)
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        /// <summary>
        /// Indicates if a Distribute operation can be performed.
        /// </summary>
        public bool CanDistribute
        {
            get
            {
                return this._listSelected.SelectedItems.Count > 0 ||
                   this._listAvailable.SelectedItems.Count > 0;
            }
        }

        #endregion Properties

        #region Constructors and Finalizers

        /// <summary>
        /// Create a ResourcePolicySystemsControl with default settings.  
        /// Must set the ResourcePolicy object before display.
        /// </summary>
        public ResourcePolicySystemsControl()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // Attach to application idle
            Application.Idle += new EventHandler(Application_Idle);
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        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
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        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

        /// <summary>
        /// Removes the update handler from the selected objects.
        /// </summary>
        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);
                }
            }
        }

        /// <summary>
        /// Fills in the list view control with the appropriate managed
        /// device objects and button display, and raises the SelectedObjectsChanged event.
        /// </summary>
        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());
            }
        }

        /// <summary>
        /// Populate the available list and the selected list.
        /// </summary>
        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 > 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);

3 个答案:

答案 0 :(得分:2)

使用ListView.Columns.Add()

答案 1 :(得分:0)

该行

this._listAvailable = new leaf.Plugins.leafListView();

暗示您的控件来自ListView(事实上,我们无法确定它可能是一个具有类似api的整个其他类)。
这使得每个答案都会被推测出来 - 你的课程可能与ListView不同(例如,它可能会改变一些方法或属性)

答案 2 :(得分:0)

问题很简单实际上一切都来了。它之所以没有在运行时出现的原因我们指的是旧的DLL而不是新的DLL ,,那么它应该怎么来,很抱歉打扰你们