Bug in ListView.BeginUpdate?

_SBradley_

Freshman
Joined
Jul 2, 2003
Messages
28
Location
Cardiff, Wales, UK
I have some code that says something like:


Code:
ListView lv = ...;
lv.BeginUpdate();

foreach (piece of data)
{
    if (an appropriate row already exists)
        item.SubItems.Add("blah");
    else
        item = lv.Items.Add("blah");
}

lv.EndUpdate();

This used to work as expected in .NET 1.0/1.1. However, since I installed 2.0 Beta 2, the 'if' part never runs; every iteration always inserts a new row, even if I've actually already inserted the specified row.

Surely, BeginUpdate/EndUpdate should only affect what's displayed, not what actually gets inserted into the ListView? Is this a bug?

Cheers. :)
 
Machaira said:
Since you've only posted pseudo-code it's hard to tell where the problem is.

Sorry, I thought that would be enough to describe the problem. Here's a "compilable snippet":

Code:
using System;
using System.Drawing;
using System.Windows.Forms;

class ListViewTest : Form
{
    static void Main()
    {
        Application.Run(new ListViewTest());
    }

    ListViewTest()
    {
        m_Grid.View = View.Details;
        m_Grid.Size = ClientSize;
        Controls.Add(m_Grid);

        PopulateGrid();
    }

    private void PopulateGrid()
    {
#if PAUSE_UPDATE
        m_Grid.BeginUpdate();
#endif
        m_Grid.Columns.Add("Col", 100, HorizontalAlignment.Left);

        foreach (string str in m_Data)
        {
            ListViewItem item = null;

            foreach (ListViewItem lvi in m_Grid.Items)
            {
                if (lvi.Text == str)
                {
                    item = lvi;
                    break;
                }
            }

            if (item == null)
            {
                item = m_Grid.Items.Add(str);
            }
        }

#if PAUSE_UPDATE
        m_Grid.EndUpdate();
#endif
    }

    private string[] m_Data = {"one", "two", "one", "three"};
    private readonly ListView m_Grid = new ListView();
}

Compile this with the .NET Framework 1.x with or without PAUSE_UPDATE defined, and you get a list containing just "one, two, three".

Compile it for the .NET Framework 2.0 without PAUSE_UPDATE defined, and you still get "one, two, three".

But compile it for the .NET Framework 2.0 with PAUSE_UPDATE defined, and you get "one, two, one, three"!

(Obviously, in the case of this snippet, a workaround would be to search the array for duplicates; but my actual app does more than this and doesn't use an array, so my only workaround right now is just to not use BeginUpdate/EndUpdate.)
 
Back
Top