Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Saving Listview into a text file..

Saving Listview into a text file..

Scheduled Pinned Locked Moved C#
tutorialquestionworkspace
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ptr2void
    wrote on last edited by
    #1

    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

    S P M 3 Replies Last reply
    0
    • P ptr2void

      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

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P ptr2void

        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

        P Offline
        P Offline
        phannon86
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • P phannon86

          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

          S Offline
          S Offline
          Simon P Stevens
          wrote on last edited by
          #4

          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

          P 1 Reply Last reply
          0
          • S Simon P Stevens

            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

            P Offline
            P Offline
            phannon86
            wrote on last edited by
            #5

            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

            S 1 Reply Last reply
            0
            • P ptr2void

              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

              M Offline
              M Offline
              MuhammadFaisal
              wrote on last edited by
              #6

              you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file

              P 3 Replies Last reply
              0
              • P phannon86

                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

                S Offline
                S Offline
                Simon P Stevens
                wrote on last edited by
                #7

                oh yeah, I must have read it too quickly. :doh:

                Simon

                1 Reply Last reply
                0
                • M MuhammadFaisal

                  you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file

                  P Offline
                  P Offline
                  ptr2void
                  wrote on last edited by
                  #8

                  The code I written there was converted from C++/CLI to C#.. So bear with me.. I can write all those for loops, that is not a problem.. Just give me an idea how to get all column data in the string builder..

                  Som

                  1 Reply Last reply
                  0
                  • M MuhammadFaisal

                    you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file

                    P Offline
                    P Offline
                    ptr2void
                    wrote on last edited by
                    #9

                    MuhammadFaisal wrote:

                    you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file

                    Thanx, I would try this..

                    Som

                    1 Reply Last reply
                    0
                    • M MuhammadFaisal

                      you have to get all other columns from lstbox by using lstbox.SubItems[index]then write them into file

                      P Offline
                      P Offline
                      ptr2void
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups