C# 添加每个项目后如何进行 ListView 更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16356092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:49:51  来源:igfitidea点击:

How can I make a ListView update after each item is added?

c#winformslistviewlogging

提问by Ben

I am trying to use a ListView to make a realtime log output to a windows form.

我正在尝试使用 ListView 将实时日志输出到 Windows 窗体。

This is the dummy code:

这是虚拟代码:

 public Form1()
    {
        InitializeComponent();
        listView1.View = View.Details;
        listView1.GridLines = false;
        listView1.Scrollable = true;

        listView1.FullRowSelect = true;
        listView1.Columns.Add("Track");
        listView1.Columns.Add("Status");

        for (int i = 1; i <= 10000; i++)
        {
            ListViewItem LVI = new ListViewItem("Track " + i);
            LVI.SubItems.Add("Updated");
            listView1.Items.Add(LVI);
            listView1.TopItem = LVI;
            listView1.EnsureVisible(listView1.Items.Count - 1);
        }
    }

How can I set it so it refreshes after each line is added? At the moment the application waits until the list is generated and then loads the form with the complete list.

如何设置它以便在添加每一行后刷新?目前,应用程序会等待列表生成,然后加载包含完整列表的表单。

采纳答案by misak

You can fill data items in another thread (for example using task):

您可以在另一个线程中填充数据项(例如使用任务):

Application.DoEvents() ... processes all window messages and redraws component.

Application.DoEvents() ... 处理所有窗口消息并重绘组件。

 public Form1()
        {
            InitializeComponent();
            listView1.View = View.Details;
            listView1.GridLines = false;
            listView1.Scrollable = true;

            listView1.FullRowSelect = true;
            listView1.Columns.Add("Track");
            listView1.Columns.Add("Status");

            Task t = new Task(new Action(() =>
                {
                    RefreshLines();
                }));
            t.Start();
        }

        public void RefreshLines()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(this.RefreshLines));
            }
            for (int i = 1; i <= 10000; i++)
            {
                ListViewItem LVI = new ListViewItem("Track " + i);
                LVI.SubItems.Add("Updated");
                listView1.Items.Add(LVI);
                listView1.TopItem = LVI;
                listView1.EnsureVisible(listView1.Items.Count - 1);
                Application.DoEvents();
            }
        }

You can call this.Refresh(); instead of Application.DoEvents();

你可以调用 this.Refresh(); 而不是 Application.DoEvents();

回答by NiklasHansen

You can call this.Invalidate()or this.Refresh()on the form to update it.

您可以致电this.Invalidate()this.Refresh()在表格上进行更新。

回答by Philip Gullick

Refresh won't work as that will only update what was already in the listview, not the added items.

刷新将不起作用,因为它只会更新列表视图中已有的内容,而不是添加的项目。

Perhaps you should look at this:

也许你应该看看这个:

Listview items not showing

列表视图项目未显示