470,632 Members | 2,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

Loosing ListView.SubItems on Sorting [#C] Win

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
May 3 '06 #1
2 2003
> 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
May 3 '06 #2
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" <ch**********@btinternet.com> wrote in message
news:eV******************@TK2MSFTNGP02.phx.gbl...
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

May 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by J_Max | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
6 posts views Thread by Dave | last post: by
1 post views Thread by Joe | last post: by
2 posts views Thread by mrmagoo | last post: by
1 post views Thread by samoore33 | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.