How to make TListView display order the same as the items were added?

54 Views Asked by At

I am adding a list of comet names and numbers into the VCL TListView and TListBox in C++Builder. I need the display order to be the order in which the names were added. This happens with the TListBox but not the TListView.

I am loading a .CSV file into a TStringList and then looping through the TStringList to load the contents into the TListBox and TListView. The TListBox displays the names in the order they are added. The TListView displays the names and numbers in two columns in an order I can't understand. With the TListView, I tried using the function Add() and Insert().

Below is my code. The picture shows the different results I get. How can I make the TListView display the names and numbers in the order they are added the same as the TListBox?

TListItem *ListItem;

std::auto_ptr<TStringList> MyCSVFile(new TStringList);
MyCSVFile->Delimiter = ',';
MyCSVFile->StrictDelimiter = true;
MyCSVFile->CommaText = "";
MyCSVFile->Clear();

MyCSVFile->LoadFromFile(GetFileName);
    

for(Row_Num=1; Row_Num < MyCSVFile->Count; Row_Num++){

    Row_string = MyCSVFile->Strings[Row_Num];
    Row_tokens->CommaText = Row_string;

    Name_string = Row_tokens->Strings[Name_Cell_Num]; 
    Num_string = Row_tokens->Strings[Num_Cell_Num]; 

    ListBox1->Items->Add(Name_string); 

    //ListView - Add()
    ListItem = ListView->Items->Add();
    ListItem->Caption = Cell_string;
    ListItem->SubItems->Add( Num_string );

    //Alternate method ListView - Insert()
    //ListItem = ListView->Items->Insert(0);
    //ListItem->Caption = Cell_string;
    //ListItem->SubItems->Insert(0, Num_string);
}

ListBox and ListView

I have tried using the Add() and Insert() functions with TListView but the display order is not the order items are added. I don't know any other way to get items into the TListView.

0

There are 0 best solutions below