Problem Saving ListView

I am using ADVListView Component although I think this might be a general problem.

My problem is that I am loading a CSV file into the listview and then sorting the data out.

This is fine, but after that I am inserting a new column and numbering this column which is fine until I save it.

When I save it and open the CSV in Excel, the new column only contains the header but no items.

Anyone have any ideas of why this is happening please?

Thanks

ST3VO
ST3VOAsked:
Who is Participating?
 
MerijnBSr. Software EngineerCommented:
the listview works a little strange regarding columns.
The first column is the .caption property of a TListItem.
The rest of them can be found in SubItems, so the content of column[3] can be found/changed in subitems[2] (note the 3 and 2, not a typo)

so if you insert a column into the listview, you should also insert an entry in the subitems for _every_ item in the listview!

so if you take the code you posted above, and change this line:
            existItem.SubItems.Add( IntToStr(ListView1.Items[z].Index) )

into:

            existItem.SubItems.Insert(0, IntToStr(ListView1.Items[z].Index) )

it should work
0
 
ST3VOAuthor Commented:
I have noticed something else with the new column on the listview.

Unlike the other columns I cannot select the data fields on the newly create Column.

Could this mean something?

Cheers

ST3VO
0
 
ST3VOAuthor Commented:
Can anyone help please? :o(
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
MerijnBSr. Software EngineerCommented:
sorry, I've never used ADVListView
0
 
Tomas Helgi JohannssonCommented:
      Hi!

Post the code where you insert the column.

Regards,
   Tomas Helgi
0
 
ST3VOAuthor Commented:
OK...I'm going to try something else that will work.

I've create a column called ID and populated the ListView with it.

The ID Column contains numbers ALL '1's'

What I need to do is replace those numbers from 1 to listview count.

How do I do this please?

Thanks

ST3VO
0
 
ST3VOAuthor Commented:
Here is the code:

procedure TForm1.Button12Click(Sender: TObject);
var
      newCol: TListColumn;
      z,j : integer;
      existItem: TListItem;
begin

      j:=0;

        //if ID Column already exists then exit
    //   if ListView1.Columns.Items[j].Caption =('ID') then exit;
         ListView1.Columns.Items[j].Caption :=('ID');
       //add a dummy ID column
    //  newCol := ListView1.Columns.Add;
    // newCol.Index:=0;
    // newCol.Caption := 'ID';

      //Now insert the numbers

      for z := 0 to (ListView1.Items.Count-1) do begin
            //find the current item in the list
            existItem := ListView1.Items[z];
         
            existItem.SubItems.Add( IntToStr(ListView1.Items[z].Index) )

end;
end;

Sorry about all the comments :o)  I've been playing around with it.

Please check my previous post as it might be easier to solve and should work!

Cheers!
0
 
xr1140Commented:

use SubItems.Add only if the column is added to the listView after you populate the list and SubItems.Insert if you have the column there from the beginning.
0
 
ST3VOAuthor Commented:
I still have problems with Saving to CSV. Although the code works, it doesn't save properly which is probably another issue.

That's why I've now added the new column, so it's there.

All I need to do now is replace the subitems of column 0 with number from 1 to listview items count.

xr1140: I've tryed Subitems.insert but I cannot get it to work either. I must be doing something wrong :o/

Can you guys help on replacing the existing subitems please?

0
 
MerijnBSr. Software EngineerCommented:
what is this CSV thing you are trying to achieve?
0
 
ST3VOAuthor Commented:
Hi MerijnB,

Thanks for responding!

I've decided to add the actual column (ID) instead of creating it at runtime because I was having problem when saving it to CSV.

Now it actually saves properly BUT I still have a problem that's driving me crazy :o/

The Existing ID Column contains all 1's as subitems + the Header (ID).

Now, I need to replace all existing subitems of the first Column (With is ID) with numbers from 1 to Listview Items Count.

This is working too BUT instead of replacing the subitems of index 0 it's replacing the subitems of index 1 on the listview.

I just cannot see what I'm doing wrong here :o(

Here is the code:

procedure TForm1.Button12Click(Sender: TObject);
var

      z : integer;
      existItem: TListItem;
begin


      ListView1.Columns.insert(0);


      //Now insert the numbers

      for z := 0 to (ListView1.Items.Count-1) do begin
            //find the current item in the list
            existItem := ListView1.Items[z];
           existItem.SubItems.Insert(0, IntToStr(ListView1.Items[z].Index) );

   end;
end;

Hope you can spot it!

Thanks a million!!!

ST3VO
0
 
MerijnBSr. Software EngineerCommented:
if it's the very first column you should set ListView1.Items[z].Caption and not one of it's subitems, is that what you mean?
0
 
MerijnBSr. Software EngineerCommented:
I'm going to bed since it's getting late here :)

If this last comment didn't help, can please explain what you try to do with the CSV data. Because I'm reading a lot on inserting colums, inserting subitems and saving, but I don't know what you want to change on the >CVS data<. Maybe there is an easier way bypassing TListView all together.
0
 
ST3VOAuthor Commented:
Nope...Still not working :o/
0
 
xr1140Commented:
there are 2 ways to do this :

1. format you CVS in such way to incorporate the ID as item and the >CVS Data< as subitems:
     - format cvs
     - add to list
     - save list


2. fill the list, add the ID (here several problems can appear depending on how you fill the list)


i'll advice choosing the first option, just let me know so i can help you
0
 
ST3VOAuthor Commented:
Thanks again xr1140.

It's been a long day!

I'll catch up tomorrow...Bedtime now!

Talk to you Tomorrow!

ST3VO
0
 
xr1140Commented:

so ? did you solve the problem ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.