Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
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);
}

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:

C#
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
Posted
Updated 12-Feb-10 11:31am
v3

1 solution

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

C#
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900