c# winforms how to save information in a listview

Lewis Joyeux 21 Reputation points
2021-02-14T03:04:06.447+00:00

I am making a stock tracker type application for a friend so that she can track the stock she sells, ive created a listview, the list has 5 columns ID, Name, Date, Items and Price. so far she can input the data into the list using the input boxes ive provided the problem is when you close out of the application and go back in everything in the list is gone and i dont know how to make it stay in there.

this application in only going to be used on her laptop and i would prefer to not make a server or database to store the data as it seems way to advanced for me however i was wondering if there was a way to save it into a txt or xml file. im very knew to this but ive been trying to figure it out for days please help.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,531 Reputation points
    2021-02-14T17:28:36.397+00:00

    For .Net the DataGridView is much better than the ListView.

    You can serialize the data into and from files that are either a delimited file (such as CSV) or a XML or a JSON file. Instead of serializing directly into and from the DataGridView, the better solution is to serialize into/from a collection and make the collection a data source for the DataGridView.

    You can load XML using ReadXml and write using WriteXml; the documentation has sample code. The following are examples of setting the data source.

    I suggest you first write a console program similar to what is in the documentation that writes out sample data then reads it back in and writes it to the console. Just to become familiar with doing that.

    Also consider using SQLite. It is a database that is installed with current versions of Windows (Windows uses it). It is lightweight and intended for uses such as this.


1 additional answer

Sort by: Most helpful
  1. Castorix31 83,206 Reputation points
    2021-02-14T06:04:01.41+00:00

    See for example Using ListViews in C# (Persistence using Streams)

    1 person found this answer helpful.
    0 comments No comments