在普通的ListView中,我通常会像这样在运行时生成列:

        var list = new List<ColumnNameSize>();
        list.Add(new ColumnNameSize("Computer Name", 0));
        list.Add(new ColumnNameSize("Full Computer Name", 150));
        list.Add(new ColumnNameSize("Migrated", 150));
        list.Add(new ColumnNameSize("Disabled", 0));
        list.Add(new ColumnNameSize("Disabled & Not Migrated", 150));
        generateListView(listViewObjects, list);

        public static void generateListView(ListView varListView, List<ColumnNameSize> nameSizeList) {
        varListView.ClearColumns();
        foreach(ColumnNameSize nameSize in nameSizeList) {
            varListView.AddColumn(nameSize.ColumnName, nameSize.ColumnSize);
        }
    }


我将如何在ObjectListView中进行操作。我尝试了多种组合:

    public void generateListView(ObjectListView varListView, List<ColumnNameSize> nameSizeList) {

        ArrayList myArrayList = new ArrayList();
        foreach (ColumnNameSize nameSize in nameSizeList) {
            OLVColumn columnHeader = new BrightIdeasSoftware.OLVColumn();
            columnHeader.Width = nameSize.ColumnSize;
            columnHeader.Text = nameSize.ColumnName;
            myArrayList.Add(columnHeader);
            varListView.AllColumns.Add(columnHeader);
        }
        Object columnsHeader = myArrayList.ToArray();
        varListView.Columns.AddRange( columnsHeader);
    }


但是似乎没有一个...

最佳答案

你可以试试这个吗?

 public void generateListView(ObjectListView varListView, List<ColumnNameSize> nameSizeList) {

        List<OLVColumn>  columnsList = new List<OLVColumn>();
        foreach (ColumnNameSize nameSize in nameSizeList) {
            OLVColumn columnHeader = new BrightIdeasSoftware.OLVColumn();
            columnHeader.Width = nameSize.ColumnSize;
            columnHeader.Text = nameSize.ColumnName;
            columnsList .Add(columnHeader);
            varListView.AllColumns.Add(columnHeader);
        }
        varListView.Columns.AddRange( columnsList.Cast<System.Windows.Forms.ColumnHeader>().ToArray());
    }

10-08 02:24