Solved

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

Posted on 2011-09-06
3
475 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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