Loosing ListView.SubItems on Sorting [#C] Win

M

MikeY

Hi everyone,

After reading various posts I'm still scratching my head and unsure of what
approach to take. I have created buttons that upon clicking, the buttons add
an item name (myName) to my ListView (lvBody) and at the same time, also add
subitems(unitPrice);

The problems is that if I don't have sort going on, then all "subitems" are
fine. If I have sorting, then the "subitems" that are being placed above the
last Alpha "subitems" info are being lost, and just the first entry and
lowest entry "subitems are being displayed, sample output ie:

UnSorted

Tuna Sand $7.95
ClubHouse $8.95
Wild Oysters $24.95
BLT $6.95

Sorted

BLT
ClubHouse
Tuna Sand $7.95
Wild Oysters $24.95

--------------------

My syntax is as follows:

this.lvBody.Sorting = SortOrder.Ascending; //Sort ListView in Ascending
Order
string unitPrice = System.String.Format("(0:C)", myPrice);

this.lvBody.BeginUpdate();
this.lvBody.Items.Add(myName);
this.lvBody.Items.[ListViewCounter].SubItems.Add(unitPrice.ToString());
this.lvBody.EndUpdate();

ListViewCounter += 1; //increments counter by 1

Now I'm unsure if I should manually sort the ListView every time an item is
added, ICompare, sort in a Collection, etc, etc. Any and all help is truly
appreciated.

MikeY
 
C

Chris Jobson

My syntax is as follows:
this.lvBody.Sorting = SortOrder.Ascending; //Sort ListView in Ascending
Order
string unitPrice = System.String.Format("(0:C)", myPrice);

this.lvBody.BeginUpdate();
this.lvBody.Items.Add(myName);
this.lvBody.Items.[ListViewCounter].SubItems.Add(unitPrice.ToString());
this.lvBody.EndUpdate();

ListViewCounter += 1; //increments counter by 1

Just a guess, but I suspect that the problem is in the two lines:
this.lvBody.Items.Add(myName);
this.lvBody.Items.[ListViewCounter].SubItems.Add(unitPrice.ToString());

Because the list is sorted, after the item has been added by the first line
it will probably be moved to the correct SORTED position in the Items array,
and thus the second line is referring to the wrong item. Try changing it to:
ListViewItem lvi = this.lvBody.Items.Add(myName);
lvi..SubItems.Add(unitPrice.ToString());

Chris Jobson
 
M

MikeY

Thanks Chris,

LOL Something so easy that one overlooks sometimes and I've been scratching
my head for the past week or so trying to figure this problem out. I worked
perfectly.

MikeY


Chris Jobson said:
My syntax is as follows:

this.lvBody.Sorting = SortOrder.Ascending; //Sort ListView in Ascending
Order
string unitPrice = System.String.Format("(0:C)", myPrice);

this.lvBody.BeginUpdate();
this.lvBody.Items.Add(myName);
this.lvBody.Items.[ListViewCounter].SubItems.Add(unitPrice.ToString());
this.lvBody.EndUpdate();

ListViewCounter += 1; //increments counter by 1

Just a guess, but I suspect that the problem is in the two lines:
this.lvBody.Items.Add(myName);
this.lvBody.Items.[ListViewCounter].SubItems.Add(unitPrice.ToString());

Because the list is sorted, after the item has been added by the first
line it will probably be moved to the correct SORTED position in the Items
array, and thus the second line is referring to the wrong item. Try
changing it to:
ListViewItem lvi = this.lvBody.Items.Add(myName);
lvi..SubItems.Add(unitPrice.ToString());

Chris Jobson
 
Top