Question Fill ListView With ArrayList from Text File

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am usually in the VB.Net forum..I am trying to convert to C# from VB.Net...

I am creating a program that the user can enter test results and save them to a text file...then they can fill the listview with the saved text file. I have the program loading an ArrayList with each line of the text file....one problem I see is the arraylist.....as it is right now it is only 5 lines long...the text file will get larger everytime the user saves the test results.

so I guess my question is , how would I get the text file contents into a list?

maybe use a List(of Integer) or List(of String)?

the text file looks like this

2/2/2013 5:14:36 PM
122
2223
223
555
this is a test Note!

here is the code I have

 private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
            StreamReader sr = new StreamReader(Application.StartupPath + [URL="file://\\SugarData.txt""]\\SugarData.txt"[/URL], False);
            string strLine = "";
            // fill the ArrayList with the contents of the text file
            ArrayList Numbers = new ArrayList();
            while (strLine != null) 
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                 
            Numbers.Add(strLine);
             
            }
            sr.Close();
            // fill variables from arrayList
            string date = Numbers[0].ToString();
            string firstNum = Numbers[1].ToString().Trim();
            string secondNum = Numbers[2].ToString();
            string thirdNum = Numbers[3].ToString();
            string fourthNum = Numbers[4].ToString();
            string note = Numbers[5].ToString();
            // fill ListView from variables
            ListViewItem lvi = new ListViewItem(date);
            lvi.SubItems.Add(firstNum);
            lvi.SubItems.Add(secondNum);
            lvi.SubItems.Add(thirdNum);
            lvi.SubItems.Add(fourthNum);
            lvi.SubItems.Add(note);
            LVResults.Items.Add(lvi);
        }


this works good...but if the text file gets larger , it doesnt......I know I need to add the text contents to something other than a ArrayList...but kinda lost now

Thanks for any Help

InkedGFX
 
Last edited:
the data is fomatted like so.

Date
test result 1
test result 2
test result 3
test result 4
note ( if the end user adds a note...so this line may be empty)

would it make more sence to combine these six lines into 1 line then split it from there?

I would like to fill the listview with this info....I have a column for the date another column for test result 1 another column for test result 2 and so on....

thank you for your help.

InkedGFX
 
Last edited:
Yes it would make more sense to have that related data on a single line, as I already said in my previous post. The most common example of that is the comma-separated values (CSV) format, where records are delimited by line breaks and fields are delimited by commas. If you need to allow commas and/or line breaks within fields then you enclose those fields in double quotes. You can then read the file line by line and split each line into fields as you go. There's actually a class that does that in VB, i.e. the TextFieldParser. You can use it in C# too and it's a good idea to do so if you do have quoted fields but, in the simple case, you may a s well just use a StreamReader and call String.Split.
 
ok....so is there a way to fill the listview with the way I have it now?

I have scoured the internet to find examples of what I am trying to do , but I cannot find any.....

Thank You for your help.

InkedGFX
 
the below code is working , except it adds the same info in each row.....

   for (int i = 0; i < Numbers.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(Numbers[0]);
                    //  lvi.Text = Numbers[1];
                    lvi.SubItems.Add(Numbers[1]);
                    lvi.SubItems.Add(Numbers[2]);
                    lvi.SubItems.Add(Numbers[3]);
                    lvi.SubItems.Add(Numbers[4]);
                    lvi.SubItems.Add(Numbers[5]);
                    LVResults.Items.Add(lvi);
                   // MessageBox.Show(Numbers[i]);
                }


I use the MessageBox to show me that the list has the info I need in it.

InkedGFX
 
yes , the point of a for loop is to use the loop counter ...but whether I use ListViewItem lvi = new ListViewItem(Numbers);
lvi.SubItems.Add(Numbers);
lvi.SubItems.Add(Numbers);
lvi.SubItems.Add(Numbers);
lvi.SubItems.Add(Numbers);
lvi.SubItems.Add(Numbers); or if I use

ListViewItem(Numbers[0]);
lvi.SubItems.Add(Numbers[1]);
lvi.SubItems.Add(Numbers[2]);
lvi.SubItems.Add(Numbers[3]);
lvi.SubItems.Add(Numbers[4]);
lvi.SubItems.Add(Numbers[5]);

the same data is added?

why is this? see screenShot below -

ListView_ScreenShot.jpg

thank You for your help..

InkedGFX
 
Last edited:
I am wondering why this code isnt working like It should...I have scoured the internet for a way to fill a listview with a List<string> and everything I have found says this code should work.....it works but not the way I expect...it fills the listview with the data from the list but it fills it in one column ..not the row ........code below

  private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
          StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]");
            
            string strLine = "";
            // fill the list<string> with the contents of the text file
           List<string> Numbers = new List<string>();
           while (strLine != null)
           {
               char delimeter = (',');
               strLine = sr.ReadLine();
               string[] data = strLine.Split(delimeter);
               for (int i = 0; i < data.Length; i++)
               {
                   Numbers.Add(data[i]);
                   ListViewItem lvi = new ListViewItem(Numbers[i]);
                   lvi.SubItems.Add(Numbers[i]);
                   LVResults.Items.Add(lvi);
                  // MessageBox.Show(Numbers[i]);
               }
              
           }
            sr.Close();
         }


I might just have to use a listbox........very frustrating....

InkedGFX
 
No matter how you turn this around, the problem you're having is to create one ListViewItem. Do you need a loop to create one item? No, loops is used to repeat things, and there is no repetition in doing something once.
 
No matter how you turn this around, the problem you're having is to create one ListViewItem. Do you need a loop to create one item? No, loops is used to repeat things, and there is no repetition in doing something once.

so if I only need one ListViewItem, how do I fill in the entire row with the data from the list...there will be six columns in the ListView....each column will have a different value.....but the text file will have an unknown amount of data.....

thank you for your help.

InkedGFX
 
the below code works..for one row......what do I do if there are an unknown amount of rows to fill?

  private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
          StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]");
            
            string strLine = "";
            // fill the list<string> with the contents of the text file
           List<string> Numbers = new List<string>();
           while (strLine != null)
           {
               char delimeter = (',');
               strLine = sr.ReadLine();
               string[] data = strLine.Split(delimeter);
               for (int i = 0; i < data.Length; i++)
               {
                   Numbers.Add(data[i]);
               }
                  
               ListViewItem lvi = new ListViewItem(Numbers[0]);
               lvi.SubItems.Add(Numbers[1]);
               lvi.SubItems.Add(Numbers[2]);
               lvi.SubItems.Add(Numbers[3]);
               lvi.SubItems.Add(Numbers[4]);
               lvi.SubItems.Add(Numbers[5]);
               LVResults.Items.Add(lvi);
               // MessageBox.Show(Numbers[i]);
               
           }
            sr.Close();
           
         }


InkedGFX
 
the below code works..for one row......what do I do if there are an unknown amount of rows to fill?
That code already works for unknown amount of rows, because you are looping all lines in file - except for the Numbers list that you trip in. Numbers list is redundant by the way, just use the 'data' array.
The same would also be true for your first file layout, where you would just make 6 consecutive calls to ReadLine each loop to create one item.
 
Thank You JohnH for your help....I removed the Numbers list and took the for loop off ....everything is working as expected now....I wouldn't have been able to fix this without your help....

Thanks Again

InkedGFX
 
Back
Top Bottom