.NET Listview Refresh

I have the following code that basically takes values ​​from a database and populates a list.

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 I'm experiencing is that it runs several times in a row (every second) and causes the items in the list to constantly disappear and reappear. Is there a way to stop viewing a list from an update before it is completed using 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
}
+5
source share
3 answers

Like this:

try
{
    lvwMyList.BeginUpdate();
    //bla bla bla

}
finally
{
    lvwMyList.EndUpdate();
}

Make sure you call lvwMyList.Items.Clear()after BeginUpdateif you want to clear the list before populating it.

+9
source

StackOverflow, .

ListView, , .

. , ListView 20 000 . ListView 20 . , ListView .

 public static async void PopulateListView<T>(ListView listView, Func<T, ListViewItem> func, 
        IEnumerable<T> objects, IProgress<int> progress) where T : class, new()
    {
        if (listView != null && listView.IsHandleCreated)
        {
            var conQue = new ConcurrentQueue<ListViewItem>();

            // Clear the list view and refresh it
            if (listView.InvokeRequired)
            {
                listView.BeginInvoke(new MethodInvoker(() =>
                    {
                        listView.BeginUpdate();
                        listView.Items.Clear();
                        listView.Refresh();
                        listView.EndUpdate();
                    }));
            }
            else
            {
                listView.BeginUpdate();
                listView.Items.Clear();
                listView.Refresh();
                listView.EndUpdate();
            }

            // Loop over the objects and call the function to generate the list view items
            if (objects != null)
            {
                int objTotalCount = objects.Count();

                foreach (T obj in objects)
                {
                    await Task.Run(() =>
                        {
                            ListViewItem item = func.Invoke(obj);

                            if (item != null)
                                conQue.Enqueue(item);

                            if (progress != null)
                            {
                                double dProgress = ((double)conQue.Count / objTotalCount) * 100.0;

                                if(dProgress > 0)
                                    progress.Report(dProgress > int.MaxValue ? int.MaxValue : (int)dProgress);
                            }
                        });
                }

                // Perform a mass-add of all the list view items we created
                if (listView.InvokeRequired)
                {
                    listView.BeginInvoke(new MethodInvoker(() =>
                        {
                            listView.BeginUpdate();
                            listView.Items.AddRange(conQue.ToArray());
                            listView.Sort();
                            listView.EndUpdate();
                        }));
                }
                else
                {
                    listView.BeginUpdate();
                    listView.Items.AddRange(conQue.ToArray());
                    listView.Sort();
                    listView.EndUpdate();
                }
            }
        }

        if (progress != null)
            progress.Report(100);
    }

IProgress, null, .

.

, ListViewItem.

public class TestListViewItemClass
{
    public int TestInt { get; set; }

    public string TestString { get; set; }

    public DateTime TestDateTime { get; set; }

    public TimeSpan TestTimeSpan { get; set; }

    public decimal TestDecimal { get; set; }
}

, . , API - , IEnumerable .

public IEnumerable<TestListViewItemClass> GetItems()
{
    for (int x = 0; x < 15000; x++)
    {
        yield return new TestListViewItemClass()
        {
            TestDateTime = DateTime.Now,
            TestTimeSpan = TimeSpan.FromDays(x),
            TestInt = new Random(DateTime.Now.Millisecond).Next(),
            TestDecimal = (decimal)x + new Random(DateTime.Now.Millisecond).Next(),
            TestString = "Test string " + x,
        };
    }
}

, , ListView, ListView. Load event ListView. , .

, ListViewItem TestListViewItemClass. , , .

private async void TestListViewForm_Load(object sender, EventArgs e)
{     
    var function = new Func<TestListViewItemClass, ListViewItem>((TestListViewItemClass x) =>
    {
        var item = new ListViewItem();

        if (x != null)
        {
            item.Text = x.TestString;
            item.SubItems.Add(x.TestDecimal.ToString("F4"));
            item.SubItems.Add(x.TestDateTime.ToString("G"));
            item.SubItems.Add(x.TestTimeSpan.ToString());
            item.SubItems.Add(x.TestInt.ToString());
            item.Tag = x;

            return item;
        }

        return null;
    });

       PopulateListView<TestListViewItemClass>(this.listView1, function, GetItems(), progress);

 }

IProgress :

progress = new Progress<int>(value =>
{
    toolStripProgressBar1.Visible = true;

    if (value >= 100)
    {
        toolStripProgressBar1.Visible = false;
        toolStripProgressBar1.Value = 0;
    }
    else if (value > 0)
    {
        toolStripProgressBar1.Value = value;
    }
 });

ListView , 12 000 ListView, . , , ListView .

, .

, , .

public static Task PopulateListViewAsync<T>(ListView listView, Func<T, ListViewItem> func,
        IEnumerable<T> objects, IProgress<int> progress) where T : class, new()
{
    return Task.Run(() => PopulateListView<T>(listView, func, objects, progress));
}
+1

false , . , reset true, .

- . left, right, height width , , true , false.

0

All Articles