Saving Listview into a text file..
-
I am having problems with saving listview items into a text file.. Here is my code:
StringBuilder sb = new StringBuilder(); for each (ListViewItem lvi in lstbox.Items) { sb.Append(lvi.Text); sb.Append(Environment.NewLine); } System.IO.StreamWriter sw = new System.IO.StreamWriter(saveSearch.FileName); for (int i = 0; i < lstbox.Items.Count -1 ; i++) sw.WriteLine(sb.ToString()); sw.Close();
But this is only showing the first column header.. I have five colums headers.. How to save all???Som
-
I am having problems with saving listview items into a text file.. Here is my code:
StringBuilder sb = new StringBuilder(); for each (ListViewItem lvi in lstbox.Items) { sb.Append(lvi.Text); sb.Append(Environment.NewLine); } System.IO.StreamWriter sw = new System.IO.StreamWriter(saveSearch.FileName); for (int i = 0; i < lstbox.Items.Count -1 ; i++) sw.WriteLine(sb.ToString()); sw.Close();
But this is only showing the first column header.. I have five colums headers.. How to save all???Som
That shouldn't even compile, "for each" should be one word "foreach". Why are you doing "Items.Count - 1" in the for loop? Why are you closing it after the first pass, if you have a good reason for that, why even use a for loop?
He who makes a beast out of himself gets rid of the pain of being a man
-
I am having problems with saving listview items into a text file.. Here is my code:
StringBuilder sb = new StringBuilder(); for each (ListViewItem lvi in lstbox.Items) { sb.Append(lvi.Text); sb.Append(Environment.NewLine); } System.IO.StreamWriter sw = new System.IO.StreamWriter(saveSearch.FileName); for (int i = 0; i < lstbox.Items.Count -1 ; i++) sw.WriteLine(sb.ToString()); sw.Close();
But this is only showing the first column header.. I have five colums headers.. How to save all???Som
for each (ListViewItem lvi in lstbox.Items) { sb.Append(lvi.Text);
loop through the lvi.SubItems collection here, and add the text of each sub item to your string builder.
sb.Append(Environment.NewLine); }
Simon
-
That shouldn't even compile, "for each" should be one word "foreach". Why are you doing "Items.Count - 1" in the for loop? Why are you closing it after the first pass, if you have a good reason for that, why even use a for loop?
He who makes a beast out of himself gets rid of the pain of being a man
Phannon wrote:
Why are you doing "Items.Count - 1" in the for loop?
More to the point, why is there even a for loop there. he's already got a for each loop to go through all the list view items and build up the string. he should just be writing out the string at this point. no second loop needed from what I can tell.
Simon
-
Phannon wrote:
Why are you doing "Items.Count - 1" in the for loop?
More to the point, why is there even a for loop there. he's already got a for each loop to go through all the list view items and build up the string. he should just be writing out the string at this point. no second loop needed from what I can tell.
Simon
I mention the loop of next line of my post, but so many head scratchers in there as to why its been done. You never know... people do weird things sometimes and think its justified. Chalk this one up to inexperience/lack of general understanding of coding on his part.
He who makes a beast out of himself gets rid of the pain of being a man
-
I am having problems with saving listview items into a text file.. Here is my code:
StringBuilder sb = new StringBuilder(); for each (ListViewItem lvi in lstbox.Items) { sb.Append(lvi.Text); sb.Append(Environment.NewLine); } System.IO.StreamWriter sw = new System.IO.StreamWriter(saveSearch.FileName); for (int i = 0; i < lstbox.Items.Count -1 ; i++) sw.WriteLine(sb.ToString()); sw.Close();
But this is only showing the first column header.. I have five colums headers.. How to save all???Som
you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file
-
I mention the loop of next line of my post, but so many head scratchers in there as to why its been done. You never know... people do weird things sometimes and think its justified. Chalk this one up to inexperience/lack of general understanding of coding on his part.
He who makes a beast out of himself gets rid of the pain of being a man
oh yeah, I must have read it too quickly. :doh:
Simon
-
you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file
-
you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file
-
you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file
MuhammadFaisal wrote:
you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file
There is nothing called subitems in listview.. Can anyone help me how to get all the strings in the listview(all headers) and save into a text file??
Som