简体   繁体   中英

C# Windows App Form, populating a listview from a text file

I've got a text file with Name, Age and Score of a player in them displayed:

Andrew
10
15
Barney
35
13 ...etc

I want the text file to be read into the listview so that it displays then name (Andrew) in the first column, age (10) in the second and score (15) in the final column and it to repeat through the text file. So it would become:

Name Age Score

Andrew 10 15
Barney 35 13

I also need these to then be able to be sorted by score so that the name/age still stay inline with the score.

Really struggling to find anything relevant to this so I'm hoping I'm not going down the wrong path. Many thanks for any help.

My code so far: https://gist.github.com/anonymous/9975832

Returns a listview with columns but no data in those columns

Replace the code in your loop with the following:

ListViewItem item = new ListViewItem();
line = reader.ReadLine();
item.Text = line;           //set first column to name

line = reader.ReadLine();
item.SubItems.Add(line);

line = reader.ReadLine();
item.SubItems.Add(line);

lsvHighscore.Items.Add(item); // add item to the list

Regarding sorting by score you can have it so that when you click the column you want to sort, the display should update.

Please see this documentation for example code to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM