saving listview data in text form

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 15
Reputation: NguyenThai is an unknown quantity at this point 
Solved Threads: 0
NguyenThai NguyenThai is offline Offline
Newbie Poster

Re: saving listview data in text form

 
0
  #11
Apr 19th, 2009
Originally Posted by JerryShaw View Post
SubString didn't work because you are using a 0 when you should be using a 1.
Looking further while to correct that.... back in a bit.
// Jerry
Using 1 at Substring does not work too. I show you example of my result. Example like I have 2 items in the ListView.
  1. ID Name
  2. 23, 45
  3. 56, 78
But the result is not what i want above, the result is below there.
  1. ID Name
  2. ,23,45
  3.  
  4. ,56,78
You can see the empty line between that and that when i use substring(0). When i used Substring(1), here is result:
  1. ID Name ,232,4545
  2. ,232,45754
No empty string but the first item is in the line of the header.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: saving listview data in text form

 
0
  #12
Apr 19th, 2009
Untested modified version
  1. private void saveButton_Click(object sender, EventArgs e)
  2. {
  3. string st1= string.Empty;
  4. FileStream file = new FileStream(@"C:\sample.playist", FileMode.Create, FileAccess.Write);
  5. StreamWriter sw = new StreamWriter(file);
  6. for (int col = 0; col < listView1.Columns.Count; col++)
  7. {
  8. st1 += (" " + listView1.Columns[col].Text.ToString() + "\t");
  9. }
  10. sw.WriteLine(st1);
  11. st1 = string.Empty;
  12.  
  13. for (int row = 0; row < listView1.Items.Count; row++)
  14. {
  15. //st1 = "\r\n"; the writeline will take care of this
  16. for (int col = 0; col < listView1.Columns.Count; col++)
  17. {
  18. // used concat operator, and string format to get things positioned right
  19. // Note that each value starts with a comma.
  20. st1 += string.Format("{0},\t", listView1.Items[row].SubItems[col].Text.ToString());
  21. }
  22. st1 = st1.Substring(0, st1.Length - 2); // remove the last comma and tab chars
  23. sw.WriteLine(st1);
  24. }
  25. sw.Flush();
  26. sw.Close();
  27. }

// Jerry
Last edited by JerryShaw; Apr 19th, 2009 at 1:52 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 15
Reputation: NguyenThai is an unknown quantity at this point 
Solved Threads: 0
NguyenThai NguyenThai is offline Offline
Newbie Poster

Re: saving listview data in text form

 
0
  #13
Apr 19th, 2009
It works with the tab and first comma, also with an empty line, but there still error, 2 duplicate first item.
ID Name
565, 87
565, 8754745, 78078
The input is
  1. ID Name
  2. 565, 87
  3. 54745, 8078
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: saving listview data in text form

 
0
  #14
Apr 19th, 2009
Here is the Tested, and working version
  1. private void saveButton_Click(object sender, EventArgs e)
  2. {
  3. string st1 = string.Empty;
  4. FileStream file = new FileStream(@"C:\Titan\sample.playist", FileMode.Create, FileAccess.Write);
  5. StreamWriter sw = new StreamWriter(file);
  6. for (int col = 0; col < listView1.Columns.Count; col++)
  7. {
  8. st1 += (" " + listView1.Columns[col].Text.ToString() + "\t"); // added \r\n
  9. }
  10. sw.WriteLine(st1);
  11. st1 = string.Empty;
  12. for (int row = 0; row < listView1.Items.Count; row++)
  13. {
  14. st1 = string.Empty;
  15. for (int col = 0; col < listView1.Columns.Count; col++)
  16. {
  17. st1 += string.Format("{0},\t", listView1.Items[row].SubItems[col].Text.ToString());
  18. }
  19. st1 = st1.Substring(0, st1.Length - 2);
  20. sw.WriteLine(st1);
  21. }
  22. sw.Flush();
  23. sw.Close();
  24. }

// Jerry
Mark as solved if this works for you.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 15
Reputation: NguyenThai is an unknown quantity at this point 
Solved Threads: 0
NguyenThai NguyenThai is offline Offline
Newbie Poster

Re: saving listview data in text form

 
0
  #15
Apr 19th, 2009
Thank for help. It works. Yeah. Can I ask you 1 question. So how we can populate the ListView with this file? My thought was we have to use delighted method of the reader for the comma, and then remove the header, and then populate each of them.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: saving listview data in text form

 
0
  #16
Apr 19th, 2009
To read the file you created, yes, discard the first line read (the header), then for each new line read, split the values based on the comma, and don't forget to replace the Tab character:

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. char[] delim = new char[2]{',','\t'};
  4. listView1.Items.Clear();
  5. TextReader file = new StreamReader(@"C:\sample.playist");
  6. file.ReadLine();
  7.  
  8. string line = file.ReadLine();
  9. string[] values;
  10. while (!string.IsNullOrEmpty(line))
  11. {
  12. values = line.Split(delim,StringSplitOptions.RemoveEmptyEntries);
  13. ListViewItem item = new ListViewItem(values[0]);
  14. for (int i = 1; i < values.Length; i++)
  15. item.SubItems.Add(values[i]);
  16. listView1.Items.Add(item);
  17. line = file.ReadLine();
  18. }
  19. file.Close();
  20. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 15
Reputation: NguyenThai is an unknown quantity at this point 
Solved Threads: 0
NguyenThai NguyenThai is offline Offline
Newbie Poster

Re: saving listview data in text form

 
0
  #17
Apr 19th, 2009
Thank you very much.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC