Using ListView’s AddRange method

L1_MMF_Reader_Listview.png

A listView control is used display realtime data from a Memory Mapped File that is being updated at a high-rate (5x/sec). This article will only focus on the updating to the listview, not the actual mechanism that performs the update on a timely basis; that will be left for another article.

My first attempt to add rows to the listView was to use the listView’s SubItem.Add method

for (i = 0; i < 1024; i++)
{
    ListViewItem row = new ListViewItem(i.ToString());
    ListViewItem bRow = new ListViewItem(i.ToString());
    ListViewItem fRow = new ListViewItem(i.ToString());

    row.SubItems.Add(arL1Int[i].ToString());
    bRow.SubItems.Add(arL1Bool[i].ToString());
    fRow.SubItems.Add(arL1Real[i].ToString());

    lvI.Items.Add(row);
    lvB.Items.Add(bRow);
    lvR.Items.Add(fRow);
}

And, even though the above code worked, it’s not the most efficient method.  Using the AddRange method is a much more efficient method.

Since each section has a boolean, integer and real data list, I first declare three generic lists:

List listViewItemsIntsList = new List&lt;ListViewItem&gt;();
List listViewItemsBoolsList = new List&lt;ListViewItem&gt;();
List listViewItemsFloatsList = new List&lt;ListViewItem&gt;();

To prevent drawing of the lists, I turn it off with a call BeginUpdate().  I turn it back on with a call to EndUpdate().

Each row in the listView is a listView item.  But, since these lists are being updated continuously, the items in the lists must first be cleared before they are populated.  Otherwise, the new rows are appended to the list.  A call to the Items.Clear() method is made to accomplish this task.

As we scan thru the array that was built from the Memory Mapped File (each array is 1024 elements), a listViewItem is created and added to the generic list.  After which the items are added to each listView via the AddRange method.

lvI.BeginUpdate();
lvB.BeginUpdate();
lvR.BeginUpdate();

lvI.Items.Clear();
lvB.Items.Clear();
lvR.Items.Clear();

for (i = 0; i < 1024; i++)
{
   ListViewItem lvi = new ListViewItem(i.ToString());
   ListViewItem lvb = new ListViewItem(i.ToString());
   ListViewItem lvf = new ListViewItem(i.ToString());

   lvi.SubItems.Add(arL1Int[i].ToString());
   lvb.SubItems.Add(arL1Bool[i].ToString());
   lvf.SubItems.Add(arL1Real[i].ToString());

   listViewItemsIntsList.Add(lvi);
   listViewItemsBoolsList.Add(lvb);
   listViewItemsFloatsList.Add(lvf);
}

lvI.Items.AddRange(listViewItemsIntsList.ToArray());
lvB.Items.AddRange(listViewItemsBoolsList.ToArray());
lvR.Items.AddRange(listViewItemsFloatsList.ToArray());

if (iTopVisIdx < 0) iTopVisIdx = 0;
if (iTopProcVisIdx < 0) iTopProcVisIdx = 0;
if (iTopDelvVisIdx < 0) iTopDelvVisIdx = 0;

lstEntryInts.TopItem = lstEntryInts.Items[iTopVisIdx];
lstEntryBools.TopItem = lstEntryBools.Items[iTopVisIdx];
lstEntryReals.TopItem = lstEntryReals.Items[iTopVisIdx];

lstProcInts.TopItem = lstProcInts.Items[iTopProcVisIdx];
lstProcBools.TopItem = lstProcBools.Items[iTopProcVisIdx];
lstProcReals.TopItem = lstProcReals.Items[iTopProcVisIdx];

lstDelvInts.TopItem = lstDelvInts.Items[iTopDelvVisIdx];
lstDelvBools.TopItem = lstDelvBools.Items[iTopDelvVisIdx];
lstDelvReals.TopItem = lstDelvReals.Items[iTopDelvVisIdx];

lvB.EndUpdate();
lvI.EndUpdate();
lvR.EndUpdate();
Blogged with the Flock Browser
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: