在ObjectListView C#中生成列

时间:2011-04-27 20:57:25

标签: c# objectlistview

在普通的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);
    }

但似乎没有工作......

3 个答案:

答案 0 :(得分:4)

你能试试吗?

 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());
    }

答案 1 :(得分:1)

要在通过代码手动添加列后使这些列可见,您需要使用以下行来重建它们:

varListView.RebuildColumns();

根据Bala的回答,您将获得以下代码:

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());
    varListView.RebuildColumns();
}

答案 2 :(得分:0)

要使ObjectListView正常工作,必须将模型对象的所需属性的名称定义为AspectName。

我宁愿一个一个地添加列,而不是先创建ColumnNameSize列表:

ObjectListView1.AllColumns.Add(new OLVColumn()
{
    Text = "Column Header",
    Width = 100,
    AspectName = "myProperty"
});

ObjectListView1.RebuildColumns();