更新listview控件,然后更新时间标记 [英] Update listview control then timet tick

查看:75
本文介绍了更新listview控件,然后更新时间标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向大家问好
有人可以帮我解决一个问题吗?
这是:
我有listview控件,计时器和数据数组str [*,*]
数组大小例如为40x2
每当计时器计时时,我都需要在listview中更新数据
我写了这段代码:

Hello to All
Can Somebody help me to solve one problem?
Here it is:
I''ve got the listview control, timer and data array str[*,*]
Array size is for example 40x2
I need to update data in listview each time when the timer ticks
I wrote this code:

private void Form1_Load(object sender, EventArgs e)
{
    str = new string[40, 2];
    for (int i = 0; i < 40; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            str[i, j] = "a" + Convert.ToString(i) + " b" + Convert.ToString(j);
        }
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    ListViewItem[] itmx = new ListViewItem[str.GetLength(0)];
    for (int i = 0; i < str.GetLength(0); i++)
    {
        itmx[i] = new ListViewItem(i.ToString());
        for (int j = 0; j < str.Length / str.GetLength(0); j++)
        {
            itmx[i].SubItems.Add(str[i, j]);
        }
    }
    listView1.Items.Clear();
    listView1.Items.AddRange(itmx);
}


但是当我刷新列表视图时,滚动条会从水平向左移动,垂直向顶部移动,并且列表视图控件会闪烁.

我尝试在此处使用虚拟模式:
http://msdn.microsoft.com/ru-ru/library/system.windows.forms.listview.virtualmode.aspx

因此,我解决了滚动条的问题,但listview仍在闪烁.

我了解,我只应更新已更改的那些单元格,但我不知道如何.
我尝试使用它:


But when i''m refresh listview, scrollbars moves, horizontal to left, and vertical to top, and listview control is blinking.

I try to use Virtual mode like here:
http://msdn.microsoft.com/ru-ru/library/system.windows.forms.listview.virtualmode.aspx

So, i solve problem with scrollbars, but listview is still blinking.

I understand, that I should update only those cells which have changed, but i don''t know how.
I try to use this:

void Torrents_list_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
    int x = e.ItemIndex;
                
    //e.Item = new ListViewItem(x.ToString());
    for (int i = 0; i < Array_data_colums; i++)
    {
        if (Torrents_list.Items[x].SubItems[i].Text!=data[x,i])
        {
            Torrents_list.Items[x].SubItems[i] = data[x, i];
        }
    }        
}


但它不起作用.
请帮助我


but it''s does not work.
Please help me

推荐答案

使用BeginUpdate和EndUpdate方法来防止列表视图闪烁.

Use the BeginUpdate and EndUpdate methods to prevent listview from blinking.

listView1.BeginUpdate();
// do the work
listView1.EndUpdate();



如果数组的大小是静态的,那么您应该能够刷新数据,而不必一遍又一遍地添加项目.



If the size of the array is static, then you should be able to refresh the data without adding the items over and over again.

for loop
{
    ListViewItem li = listview1.Items[i];
    li.Text = str[i,j];
    another for
    {
        li.SubItems[j].Text = str[i,j];
    }
}



希望你有主意...



Hope you get the idea...


这篇关于更新listview控件,然后更新时间标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆