0

How can I save the the data in a listview?

For example if the user has put something into the listview and then closes the application, all that information is then lost. Is there any way of saving the information as soon as they put something into the listView?

This is the code I use to input the information from a textbox to the listView1:

  string[] items = { titletxt.Text, statustxt.Text };
        ListViewItem lvi = new ListViewItem(items);
        listView1.Items.Add(lvi);

UPDATE (29/12/2012)

Thanks for all the help! But I can't seem to get it working. I have created a really simple form to try and get it working. It has 3 textboxes (NameTxt, AgeTxt, HairColourTxt), a button, and the listview(ListOfPeople).

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace listview
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] items = { NameTxt.Text, AgeTxt.Text, HairColourTxt.Text };
            ListViewItem lvi = new ListViewItem(items);
            ListOfPeople.Items.Add(lvi);
        }
    }
}

I have tried to add the code posted by @user574632 & @Joe & @DmitryKvochkin and I have changed bits but still cant get it to work. I'm not sure what I'm doing wrong? I have tried to add this:

     private void saveListOfPeopleItems(string path, ListView lv)
    {
        var delimeteredListviewData = new List<string>();

        foreach (ListViewItem lvi in lv.Items)
        {
            string delimeteredItems = string.Empty;
            foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
            {
                delimeteredItems += lvsi.Text + "#";
            }
            delimeteredListviewData.Add(delimeteredItems);
        }

        System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
    }

    private void loadListOfPeopleItems(string path, ListView lv)
    {

        foreach (string line in System.IO.File.ReadAllLines(path))
        {
            lv.Items.Add(new ListViewItem(line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)));
        }
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Giovanni
  • 580
  • 2
  • 11
  • 31
  • 2
    take a look at this if this link doesn't work out for you do a google search on how to Serialize listview data, http://stackoverflow.com/questions/2292480/xmlserializer-list-item-element-name – MethodMan Dec 28 '12 at 21:33
  • You have to put the information in somewhere like a database or a regular file (e.g. a .txt or .dat file). Since you're using a listview that may contain an item with subitems, xml serialization is probably the best way to do it as suggested in other comments. – Arian Motamedi Dec 28 '12 at 21:39
  • How are you calling the load and save routines? Should be for example loadListOfPeopleItems("c:\path\to\file.txt", ListOfPeople); – Steve Dec 29 '12 at 15:10
  • Thanks for the reply! I haven't done that, I have just written the code above and changed "loadListOfPeopleItems(string...". How should I call the load and save routines? Is there a way of doing this in the properties of visual studios? – Giovanni Dec 29 '12 at 15:23

4 Answers4

1

You need to create the save logic, there's no automatic method.

The easiest way is to use Serialization (look for ISerializable) to save to a file of your choice (name it whatever you like): You can save entire objects this way. Here is a simple tutorial on serialization.

The other method is to parse the listview content into strings (keep only what you need), and you save the strings to a text file (TextReader and TextWriter).

If you want to save application settings (and not user data), have a look at this link, or this one which might be easier to read.

Finally, if you need to interact with the data you save regularly, or if you have a lot of data to store, use a database (SQL, mySQL, etc). The latter method is the longest one to implement.

Joe
  • 2,496
  • 1
  • 22
  • 30
  • Thank you for the reply, Im still learning all this :D How do I do Serialization? – Giovanni Dec 28 '12 at 21:54
  • I have added a link to a serialization tutorial. If you have custom objects, you will have to implement ISerializable on each object. Seeing your original code, you seem to only have 2 strings to save? TextWriter might be an easier method for you. Or use the integrated settings of Visual Studio (I will add a link shortly). – Joe Dec 28 '12 at 22:02
  • I have added 2 links. Consider accepting the answer that best fits your scenario / that best helped you, in order to help us! :) – Joe Dec 28 '12 at 22:08
  • Thank you for the help!, I recon the best method would be to save the information to a text file. There inst much information to store. – Giovanni Dec 28 '12 at 22:24
1

if you want to save to a text file, you could use the following:

 private void saveListViewItems(string path, ListView lv)
    {
        var delimeteredListviewData = new List<string>();

        foreach (ListViewItem lvi in lv.Items)
        {
            string delimeteredItems = string.Empty;
            foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
            {
                delimeteredItems += lvsi.Text + "#";
            }
            delimeteredListviewData.Add(delimeteredItems);
        }

        System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
    }

    private void loadListViewItems(string path, ListView lv)
    {

        foreach (string line in System.IO.File.ReadAllLines(path))
        {
            lv.Items.Add(new ListViewItem(line.Split(new char[]{'#'}, StringSplitOptions.RemoveEmptyEntries)));
        }
    }
Steve
  • 20,703
  • 5
  • 41
  • 67
  • Thanks for this! But its not working for my code. I think there is something im doing wrong. Because im still learning all this could you please explain the things that I need to change to suite my code. Thank you for the help! :D – Giovanni Dec 29 '12 at 14:59
0

You can use integrated visual studio settings and than bind it to your listview, even in designer. After you changed your listview you can save them

VladL
  • 12,769
  • 10
  • 63
  • 83
-1

I would say the answer is : use .txt files Also you can create an SQL database, and save it there...

DmitryK
  • 1,333
  • 1
  • 11
  • 18
  • Thanks for the response :D How can I store the information to a .txt file and then retrieve it once the application has been reopened? – Giovanni Dec 28 '12 at 22:29
  • Well, every time you run the application, the first thing to do is to read from .txt file to your "list view". Every time you close the application, you should run a function that overwrites(deletes the existing one, create new one with the same name) the .txt file with new "list view". You can check for the code here: http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx – DmitryK Dec 28 '12 at 22:38