943,712 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2469
  • C# RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 19th, 2009
0

Re: saving listview data in text form

Click to Expand / Collapse  Quote originally posted by JerryShaw ...
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.
C# Syntax (Toggle Plain Text)
  1. ID Name
  2. 23, 45
  3. 56, 78
But the result is not what i want above, the result is below there.
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  1. ID Name ,232,4545
  2. ,232,45754
No empty string but the first item is in the line of the header.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NguyenThai is offline Offline
15 posts
since Sep 2006
Apr 19th, 2009
0

Re: saving listview data in text form

Untested modified version
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 19th, 2009
0

Re: saving listview data in text form

It works with the tab and first comma, also with an empty line, but there still error, 2 duplicate first item.
Quote ...
ID Name
565, 87
565, 8754745, 78078
The input is
C# Syntax (Toggle Plain Text)
  1. ID Name
  2. 565, 87
  3. 54745, 8078
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NguyenThai is offline Offline
15 posts
since Sep 2006
Apr 19th, 2009
0

Re: saving listview data in text form

Here is the Tested, and working version
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 19th, 2009
0

Re: saving listview data in text form

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NguyenThai is offline Offline
15 posts
since Sep 2006
Apr 19th, 2009
0

Re: saving listview data in text form

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:

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 19th, 2009
0

Re: saving listview data in text form

Thank you very much.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NguyenThai is offline Offline
15 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Automatically detect CD/DVD
Next Thread in C# Forum Timeline: how to make chats in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC