Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have some code that says something like:

 

 


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();[/Code]

 

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. :)

Posted
  Quote
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":

 

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.)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...