Solved

Save and load Listview content

Posted on 2010-11-21
4
606 Views
Last Modified: 2013-11-26
HI,

I have the methode for saving the listview content to file.
But I don't have the methode for loading the content of the file to listview.

Who can help me?

Greetings, Peter Kiers
private void button3_Click(object sender, EventArgs e)
        {
            TextWriter tw = new StreamWriter("Data\\Glucose.sav");
            StringBuilder listViewContent = new StringBuilder();
            for (int item = 0; item < this.lstvGlucose.Items.Count; item++)
            {
                for (int subitem = 0;
                   subitem < this.lstvGlucose.Columns.Count;
                   subitem++)
                {
                    listViewContent.Append
                    (this.lstvGlucose.Items[item].SubItems[subitem].Text);
                    if (subitem < this.lstvGlucose.Columns.Count - 1)
                        listViewContent.Append(",");
                }
                tw.WriteLine(listViewContent);
                listViewContent = new StringBuilder();
            }
            tw.Close();
        }

Open in new window

0
Comment
Question by:peterkiers
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34183232
may I ask why are you asking the same question again?
0
 
LVL 1

Accepted Solution

by:
peterkiers earned 0 total points
ID: 34183467
Found it:

Greetings, Peter Kiers
private void button3_Click(object sender, EventArgs e)
        {
            FileStream fsWrite = File.Open("Glucose.dat", FileMode.Create, FileAccess.ReadWrite);
            BinaryFormatter binfmt = new BinaryFormatter();
            binfmt.Serialize(fsWrite, new ArrayList(lstvGlucose.Items));
            fsWrite.Close();
        }
        /*--------------------------------------------------------------------*/
        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fsRead = File.Open("Glucose.dat", FileMode.Open, FileAccess.ReadWrite);
            binfmt = new BinaryFormatter();
            ArrayList deserialized_array_list = (ArrayList)binfmt.Deserialize(fsRead);
            Array listviewitem_array = (deserialized_array_list).ToArray(typeof(ListViewItem));
            lstvGlucose.Items.AddRange((ListViewItem[])listviewitem_array);
            fsRead.Close();
        }
        /*--------------------------------------------------------------------*/

Open in new window

0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34195200
cancel the question to save your points
0
 
LVL 1

Author Closing Comment

by:peterkiers
ID: 34216127
This is the best solution because it works great and fast.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now