views:

57

answers:

3

I have the following code, which basically takes values from a database and populates a listview.

using (IDataReader reader = cmd.ExecuteReader())
{                    
    lvwMyList.Items.Clear();
    while (reader.Read())
    {
        ListViewItem lvi = lvwMyList.Items.Add(reader["Value1"].ToString());
        lvi.SubItems.Add(reader["Value2"].ToString());                    
    }
}

The problem that I have is that this is repeatedly executed at short intervals (every second) and results in the items in the listview continually disappearing and re-appearing. Is there some way to stop the listview from refreshing until it’s done with the updates? Something like below:

using (IDataReader reader = cmd.ExecuteReader())
{                    
    lvwMyList.Items.Freeze(); // Stop the listview updating
    lvwMyList.Items.Clear();
    while (reader.Read())
    {
        ListViewItem lvi = lvwMyList.Items.Add(reader["Value1"].ToString());
        lvi.SubItems.Add(reader["Value2"].ToString());                    
    }
    lvwMyList.Items.UnFreeze(); // Refresh the listview
}
+2  A: 
lvwMyList.BeginUpdate();
//bla bla bla
lvwMyList.EndUpdate();
jgauffin
It's still gonna 'flash' when you clear items. Same happens on TreeView.
leppie
This definately does what I asked for. Only problem is that it virtually locks the form as well :-)
pm_2
Having clear inside beginupdate should prevent it from flashing. The form is not locked by beginupdate but by your code that adds the new items. Try fetching all items from the Db before doing the update.
jgauffin
A: 

This is a 'known' probably with the ListView (actually the underlying Win32 class).

A better approach would be to use a DataGridView, make it look like a ListView (not hard), and then use a BindingList as a datasource.

leppie
A: 

You can also try setting the visible or enabled properties to false during the update and see if you like those results any better. Of course, reset the values to true when the update is done.

Another approach is to create a panel to overlay the listbox. Set it's left, right, height, and width properties the same as your listbox and set it's visible property to true during the update, false after you're done.

Beth
Disabling and enabling the control seems to make the problem worse
pm_2