在objectlistview中编辑文本的正确方法是什么

问题描述 投票:0回答:2

我有一个包含4列和动态行数的objectlistview,我正在努力编译单元格文本值,并可选择更改前景色

我已经阅读了所有可以放手的东西和任何东西,但是找不到任何有效且正确的关于如何做到这一点的例子。

这就是为什么要创建ObjectListView

List<VideoItem> list = new List<VideoItem>();
foreach (dynamic item in VideoItems)
{
    list.Add(new VideoItem { Index = (int)item.index, OldName = (string)item.oldname, NewName = (string)item.newname });
}


olv1.AddObjects(list);

VideoItem类看起来像这样

private class VideoItem
{
    public int Index;
    public string OldName;
    public string NewName;
}

enter image description here

objectlistview
2个回答
1
投票

但我需要可编程地编辑事件上的单元格文本。我正在对其他单元格进行一些逻辑运算,我将结果存储到它旁边的单元格中。

您应该将结果(进行更改)存储到基础模型对象,然后调用RefreshObject(myModelObject);

关于forcolor,我只需要改变我改变的单元格

“要更改单个单元格的格式,需要将UseCellFormatEvents设置为true,然后侦听FormatCell事件。”

看看at this


0
投票

只是要添加到Rev1.0答案,我需要更新包含项目的对象(在我的情况下是一个List)然后,使用olv1.RefreshObject(list);流程由olv1.BuildList(true); olv1.BuildList(true);立即刷新GUI。

这里有一个小代码片段,可以让事情更清楚,当选中复选框时,它会更改第3列中的数据。

using System.Collections.Generic;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Initializeolv();
        }

        private class VideoItem
        {
            public int Index;
            public string OldName;
            public string NewName;
        }
        private List<VideoItem> list = new List<VideoItem>();
        private void Initializeolv()
        {

            for (var i = 1; i <= 10; i++)
            {
                list.Add(new VideoItem { Index = i, OldName = $"old{i}", NewName = $"new{i}" });
            }


            olv1.AddObjects(list);


        }

        private void olv1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            list[e.Item.Index].NewName = "new200";


            olv1.RefreshObject(list);
            olv1.BuildList(true);


        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.