[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Save and load content of listview to file.

Posted on 2010-11-21
7
Medium Priority
?
695 Views
Last Modified: 2013-12-17
Hi,

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

Who can help me?

Peter

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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34182859
are you converting the contents of the listview into aaa,bbb,ccc then saving the result to a file?
if so, here is an easy way to save then load the contents using String.Split method:
this code is for a single-column listview but it can work for multiple columns with few changes
// saving:
StringBuilder listViewContent = new StringBuilder();
foreach (ListViewItem item in listView1.Items)
	listViewContent.Append(item.Text + ",");

if(listViewContent.Length > 0) 
	listViewContent.Length = listViewContent.Length - 1; // remove the comma at the end

File.WriteAllText("Data\\Glucose.sav", listViewContent.ToString());

// loading:
string savedContents = File.ReadAllText("Data\\Glucose.sav");

ListView.ListViewItemCollection ls = new ListView.ListViewItemCollection(listView1); // gets the listview contents as a collection
ls.Clear();

foreach (string s in savedContents.Split(','))
	ls.Add(s);

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 34182863
Hi, abdkhlaif.
I rather stick to my code. Because now I have the saving part working OK.
Only I am missing the loading part.

greetings, Peter Kiers
0
 
LVL 3

Accepted Solution

by:
abdkhlaif earned 2000 total points
ID: 34182937
you can still use the String.Split to convert a comma seperated values into a string array.
use this code for loading:

assuming that:
contents of file Data\\Glucose.sav:
a1,a2,a3,a4
b1,b2,b3,b4
c1,c2,c3,c4

and the listview has 4 columns
int rowcount = 0;
foreach (string line in File.ReadAllLines("Data\\Glucose.sav"))
{
	string[] items = line.Split(','); // convert line into a string array
	listView1.Items.Add(items[0]); // add row

	for (int i = 1; i < items.Length; i++) // skip the first item because its already added
		listView1.Items[rowcount].SubItems.Add(items[i]);
	rowcount++;
}

Open in new window

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 1

Author Comment

by:peterkiers
ID: 34183102
My save methode uses a delimeter too.
P.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34183103
I just want the loading part of it.

Greetings, Peter Kiers
0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34183106
the code in my last comment is for loading only
0
 
LVL 1

Author Comment

by:peterkiers
ID: 34183172
Oke. Thanks.

Peter
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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