| | |
saving listview data in text form
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
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
C# Syntax (Toggle Plain Text)
ID Name 23, 45 56, 78
C# Syntax (Toggle Plain Text)
ID Name ,23,45 ,56,78
C# Syntax (Toggle Plain Text)
ID Name ,232,4545 ,232,45754
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Untested modified version
// Jerry
C# Syntax (Toggle Plain Text)
private void saveButton_Click(object sender, EventArgs e) { string st1= string.Empty; FileStream file = new FileStream(@"C:\sample.playist", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(file); for (int col = 0; col < listView1.Columns.Count; col++) { st1 += (" " + listView1.Columns[col].Text.ToString() + "\t"); } sw.WriteLine(st1); st1 = string.Empty; for (int row = 0; row < listView1.Items.Count; row++) { //st1 = "\r\n"; the writeline will take care of this for (int col = 0; col < listView1.Columns.Count; col++) { // used concat operator, and string format to get things positioned right // Note that each value starts with a comma. st1 += string.Format("{0},\t", listView1.Items[row].SubItems[col].Text.ToString()); } st1 = st1.Substring(0, st1.Length - 2); // remove the last comma and tab chars sw.WriteLine(st1); } sw.Flush(); sw.Close(); }
// Jerry
Last edited by JerryShaw; Apr 19th, 2009 at 1:52 am.
•
•
Join Date: Sep 2006
Posts: 15
Reputation:
Solved Threads: 0
It works with the tab and first comma, also with an empty line, but there still error, 2 duplicate first item.
The input is
•
•
•
•
ID Name
565, 87
565, 8754745, 78078
C# Syntax (Toggle Plain Text)
ID Name 565, 87 54745, 8078
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Here is the Tested, and working version
// Jerry
Mark as solved if this works for you.
C# Syntax (Toggle Plain Text)
private void saveButton_Click(object sender, EventArgs e) { string st1 = string.Empty; FileStream file = new FileStream(@"C:\Titan\sample.playist", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(file); for (int col = 0; col < listView1.Columns.Count; col++) { st1 += (" " + listView1.Columns[col].Text.ToString() + "\t"); // added \r\n } sw.WriteLine(st1); st1 = string.Empty; for (int row = 0; row < listView1.Items.Count; row++) { st1 = string.Empty; for (int col = 0; col < listView1.Columns.Count; col++) { st1 += string.Format("{0},\t", listView1.Items[row].SubItems[col].Text.ToString()); } st1 = st1.Substring(0, st1.Length - 2); sw.WriteLine(st1); } sw.Flush(); sw.Close(); }
// Jerry
Mark as solved if this works for you.
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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)
private void button2_Click(object sender, EventArgs e) { char[] delim = new char[2]{',','\t'}; listView1.Items.Clear(); TextReader file = new StreamReader(@"C:\sample.playist"); file.ReadLine(); string line = file.ReadLine(); string[] values; while (!string.IsNullOrEmpty(line)) { values = line.Split(delim,StringSplitOptions.RemoveEmptyEntries); ListViewItem item = new ListViewItem(values[0]); for (int i = 1; i < values.Length; i++) item.SubItems.Add(values[i]); listView1.Items.Add(item); line = file.ReadLine(); } file.Close(); }
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
Other Threads in the C# Forum
- Previous Thread: Automatically detect CD/DVD
- Next Thread: how to make chats in C#
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





