11 Replies - 847 Views - Last Post: 02 March 2013 - 02:12 AM Rate Topic: -----

#1 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Saving and loading ListView items

Posted 01 March 2013 - 02:55 AM

Hey guys,

So I've been trying to save a listview items to a txt file - since I'm not that experienced with any known database yet -, but it seems that I did something wrong somewhere in my code and it doesn't save the date. It does create the file, but no data inside. Here is my code:
try
            {
                StringBuilder sb = new StringBuilder();
                StreamWriter sw = new StreamWriter("Log.txt");

                if (logListView.Items.Count > 0)
                {
                    foreach (ListViewItem lvi in logListView.Items)
                    {
                        foreach (ListViewItem.ListViewSubItem listViewSubItem in lvi.SubItems)
                        {
                            sb.Append(string.Format("{0}\t", listViewSubItem.Text));
                        }
                        sw.WriteLine(sb.ToString());
                    }
                    sw.WriteLine();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Something went wrong, please try again!", "Error",MessageBoxButtons.OK ,MessageBoxIcon.Error);
            }



So any suggestions guys?

Is This A Good Question/Topic? 0
  • +

Replies To: Saving and loading ListView items

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 313
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Saving and loading ListView items

Posted 01 March 2013 - 05:47 AM

Are you sure you want to output the listview sub items and not the listview items? Because that's what you're trying. Try taking out the foreach loop on line 10 and try again.
Was This Post Helpful? 0
  • +
  • -

#3 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Re: Saving and loading ListView items

Posted 01 March 2013 - 01:03 PM

so I deleted the sub items and just save the items like in this code
  if (logListView.Items.Count > 0)
                {
                    foreach (ListViewItem lvi in logListView.Items)
                    {
                        sb.Append(logListView.Items);
                        sw.WriteLine(sb.ToString());
                    }
                    sw.WriteLine();
                }

but it still create a file, but no data is being saved.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Saving and loading ListView items

Posted 01 March 2013 - 01:44 PM

Do you have at least one item so the the if statement is satisfied?

Are you closing your stream writer to cause it to flush the data?

Are you sure you are looking is the right place for the output file? If so, can you tell us where you are looking?
Was This Post Helpful? 0
  • +
  • -

#5 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Re: Saving and loading ListView items

Posted 01 March 2013 - 01:57 PM

what do you mean by looking, and yes I have at lease on item.

Yes I was missing the close statement which cause a problem and I fixed that.
StringBuilder sb = new StringBuilder();
                StreamWriter sw = new StreamWriter("Log.txt");

                if (logListView.Items.Count > 0)
                {
                    foreach (ListViewItem lvi in logListView.Items)
                    {
                        sb.Append(logListView.Items.ToString());
                        sw.WriteLine(sb);
                    }
                    sw.WriteLine();
                }
                sw.Close();


however the output was
System.Windows.Forms.ListView+ListViewItemCollection


and it suppose to be like this.
$1245   $124   0.20   2


just like the listview items are
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3724
  • View blog
  • Posts: 12,990
  • Joined: 12-December 12

Re: Saving and loading ListView items

Posted 01 March 2013 - 02:22 PM

foreach (ListViewItem lvi in logListView.Items)

You are looping through the listviewitems as lvi but you are attempting to output the original Items collection as a string. You should be making use of lvi within your loop.
Was This Post Helpful? 0
  • +
  • -

#7 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Re: Saving and loading ListView items

Posted 01 March 2013 - 02:30 PM

well I haven't used lvi before, so can you please direct me to a place where it explain how will this work?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3724
  • View blog
  • Posts: 12,990
  • Joined: 12-December 12

Re: Saving and loading ListView items

Posted 01 March 2013 - 02:36 PM

View Postamture106, on 01 March 2013 - 02:30 PM, said:

well I haven't used lvi before, so can you please direct me to a place where it explain how will this work?

http://msdn.microsof...6(v=vs.80).aspx
Was This Post Helpful? 0
  • +
  • -

#9 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Re: Saving and loading ListView items

Posted 01 March 2013 - 04:59 PM

well I fixed the foreach like this code
 StringBuilder sb = new StringBuilder();
                StreamWriter sw = File.AppendText("LogFile.txt"); ;
                string item;

                if (logListView.Items.Count > 0)
                {
                    foreach (ListViewItem lvi in logListView.Items)
                    {
                        sw.WriteLine(lvi);
                        sw.Close();
                    }
                }



however I have 4 items in a row and I want to save all of those in one row in the text file Posted Image, so I wrote a for loop that iterates 4 time, but i get an error each time.

any ideas?

and the output text in the text file is
ListViewItem: {$17.42}
and I want it to look like this
$17.42   $784.00   44.00   1.0

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: Saving and loading ListView items

Posted 01 March 2013 - 09:21 PM

Instead of just throwing the ListViewItem to WriteLine(), you'll have to pick out the Text property, and each of the SubItem's Text from the ListViewItem and write them out. In your attempt on post #1 it looks like you already knew about the SubItems, so it's just a matter of coupling that knowledge with the Text property of the primary item.

I always recommend taking time to read the documentation in MSDN instead of just flailing around randomly with Intellisense. You would have probably figured this out sooner if you had.
http://msdn.microsof...stviewitem.aspx
Was This Post Helpful? 0
  • +
  • -

#11 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 156
  • Joined: 08-September 11

Re: Saving and loading ListView items

Posted 01 March 2013 - 09:33 PM

well after I read that my first post was correct, but all I needed is to add
sw.Close()
so just for the future reference here's a working code to save ListView items into a text file
try
            {
                StringBuilder sb = new StringBuilder();
                StreamWriter sw = File.AppendText("LogFile.txt"); ;
                string item;

                if (logListView.Items.Count > 0)
                {
                    foreach (ListViewItem lvi in logListView.Items)
                    {
                        foreach (ListViewItem.ListViewSubItem listViewSubItem in lvi.SubItems)
                        {
                            sb.Append(string.Format("{0}\t", listViewSubItem.Text));
                        }
                        sw.WriteLine(sb.ToString());
                        sw.Close();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Something went wrong, please try again!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3724
  • View blog
  • Posts: 12,990
  • Joined: 12-December 12

Re: Saving and loading ListView items

Posted 02 March 2013 - 02:12 AM

Are you sure this is fully working? You are closing the StreamWriter within a loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1