Solved

Load and save the data of a listview to a file as binary

Posted on 2011-09-06
3
471 Views
Last Modified: 2013-12-17
Dear Experts,

I am looking for code to load and save the data of the listview columns
to file as binary.

Who knows the anwer or an example and is willing to help me?

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 36490281
Hello buddy, I did this example for you, it show you how to take the items from your ListView instance and save it to a file on disk, also how to restore the items from that file to the ListView:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SaveListViewItems
{
    public partial class Form1 : Form
    {
        Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            // Generate random data...
            for (int i = 0; i < 6; i++)
            {
                ListViewItem item = listView1.Items.Add(DateTime.Now.ToString());
                item.SubItems.Add(rnd.Next().ToString());
                item.SubItems.Add(rnd.Next().ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = "*.bin|*.bin";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ListViewItem[] items = new ListViewItem[listView1.Items.Count];
                    listView1.Items.CopyTo(items, 0);

                    // Serialize the items and save it to a file.
                    using (System.IO.FileStream fs = System.IO.File.Create(dlg.FileName))
                    {
                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        bf.Serialize(fs, items);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "*.bin|*.bin";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ListViewItem[] items = null;

                    // Deserialize the items from the binary file.
                    using (System.IO.FileStream fs = System.IO.File.OpenRead(dlg.FileName))
                    {
                        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                        items = (ListViewItem[])bf.Deserialize(fs);
                    }

                    listView1.Items.Clear();
                    // Load the items into the listView1.Items collection.
                    listView1.Items.AddRange(items);
                }
            }
        }
    }
}

Open in new window


You can download the full project from this link:
http://dl.dropbox.com/u/13237304/EE/SaveListViewItems.zip

In this project you can generate random data to test, load and save the items from the ListView to a binary file on disk.

I hope this help.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 36490900
Ahh, thanks alot. good example.

Greetings,

Peter Kiers
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36491002
Glad to help!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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