Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

20 Experts available now in Live!

Get 1:1 Help Now