在 ObjectListView C# 中生成列

发布于 2024-11-03 20:51:26 字数 1571 浏览 0 评论 0原文

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

How will I do it in 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);
    }

但似乎都不起作用......

In normal ListView I usually generate columns on fly with something like:

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

How would I do it in ObjectListView. I've tried multiple combinations:

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

But none seems to work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩世 2024-11-10 20:51:26

你能试试这个吗?

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

Can you try this?

 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());
    }
℉絮湮 2024-11-10 20:51:26

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

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

To make the columns visible after adding them manually through code, you need to rebuild them with the following line:

varListView.RebuildColumns();

Based on Bala's answer, you get the following code:

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();
}
临走之时 2024-11-10 20:51:26

为了使 ObjectListView 按预期工作,您必须将模型对象所需属性的名称定义为 AspectName。

我不想首先创建 ColumnNameSize 列表,而是一一添加列:

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

ObjectListView1.RebuildColumns();

For the ObjectListView to work as intended, you have to define the name of the desired property of your model object as AspectName.

Instead of first creating a list of ColumnNameSize, I'd rather add the columns one by one:

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

ObjectListView1.RebuildColumns();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文