Hi ppl. Im a bit of a newbee when it comes to coding but having a problem adding a text file to a listview box. My code sort of works as far as putting data in the first collum goes.

// Set variables
            int collum = 0;
            int arraySize = 0;
            
            // Read codes.txt in to codeNew
            FileStream codeNew = File.Open("C:\\codes.txt", FileMode.Open, FileAccess.Read);
            StreamReader codeRead = new StreamReader(codeNew);
            string codeView = codeRead.ReadToEnd();
            
            // release variables
            codeRead.Close();
            codeNew.Close();

            //Capture parts of codes.txt between the * and put into array
            String[] codeArray = codeView.Split(new char[] { '*' });

            //Put contents of array into three collum listview1
            do
            {
                string code = codeArray[collum];
                listView1.Items.Add(code);
                code = codeArray[collum + 1];
                listView1.Items.subItems.add(code);
                code = codeArray[collum + 2];
                listView1.Items.subItems.add(code);

                collum = collum + 3;
                arraySize=arraySize +3;
            } while (arraySize < (codeArray.Length));

The red bits are whats giving me errors. I have similar lines of code earlier in my proggy that works but here I get error "System.Windows.Forms.ListView' does not contain a definition for 'subitems'"

can someone tell me what im doing wrong or how to add the data to collums 2 and 3 please?

eg of codes.txt:
collum1*collum2*collum3*collum1*collum2*collum3*collum1*collum2*collum3 etc.

ok so i solved it my self

do
            {
                string code = codeArray[collum];
                ListViewItem item = new ListViewItem(code);
                code = codeArray[collum + 1]; 
                item.SubItems.Add(code);
                code = codeArray[collum + 2];
                item.SubItems.Add(code);
                cTotal = cTotal + double.Parse(code);
                listView1.Items.Add(item);
                collum = collum + 3;
                arraySize = arraySize +3;
            } while (arraySize < (codeArray.Length));
            txtTCost.Text = cTotal.ToString();

caio

Yep, this is how to be done. You have create a new instance of a class ListViewItem. And when ever you want to add subitems to it, you have to call the newly created instance of a class and then called it`s subitem class to add some value into a 2nd, 3rd, 4th, ... column of a listView row.

thanks for the info. im also going to want to save the data to the text file too. adding any extra lines iv put in to the listview. i may end up posting for help on that but im going to try and figure that out first. any tips or an idea on what command to use would be helpfull altho im sure i could find a thred on here to help with that somewhere.
thanks again.

What kind of text are you going to write into text fine? Only some lines, or there will be plenty of text? Iam asking, because there are different alternatives for each way.

This is a simple file creator (write line by line):

string path = @"\C:\test.txt";
StreamWriter sw;
sw = File.CreateText(path);
sw.WriteLine("1st line of a 1st text");
sw.Write("1st line of 2nd text");
sw.WriteLine("2nd line of the text");
sw.Flush(); //end of file!
sw.Close();

This is how you can create and write to a file a string:

StreamWriter sw = new StreamWriter(@"c:\test.txt");
string lines = "First line.\r\nSecond line.\r\nThird line.";
sw.WriteLine(lines);
sw.Close();

the same way you can write an array:

StreamWriter sw = new StreamWriter(@"c:\test.txt");
string lines = "First line.\r\nSecond line.\r\nThird line.";
sting[] array = lines.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < array.Length; i++)
{
   sw.WriteLine(array[i]);
}
sw.Flush();
sw.Close();

wow thanks. plenty of food for thought. i guess as the listview getts populated there will be plenty to text to add.
so far my text file has no carrige returns. i couldnt figure out how to omit the \r\n so i put all the text is on one line. i guess it will be hard to read as there gets more and more data in there.

well im gonna spend some time trying to implement ur code now. thanks for ur help.
ill let you know how i get on :)

hi Mitja done it now thanks. this is what i ended up with

{
            int subitemCount = 3;
            StreamWriter sw = new StreamWriter(@"c:\test.txt");
            string[] itemData = new string[subitemCount];
            foreach (ListViewItem item in listView1.Items)
            {
                for (int nIdx = 0; nIdx < subitemCount; ++nIdx)
                    itemData[nIdx] = item.SubItems[nIdx].Text;
                sw.WriteLine(String.Join("*", itemData));
            };
            sw.Flush();
            sw.Close();
        }

now i just got to read it back in to the listview as writing format differs from my original txt file
speak to you l8r

This is simple (again :))
You can use StreamReader to retrive data from file back to listView. This is how:

private void ReadingFromFile()
        {            
            using (StreamReader sr = new StreamReader(@"C:\2\testFile2.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    ListViewItem lvi = new ListViewItem(array[0].ToString());
                    lvi.SubItems.Add(array[1]);
                    this.listView1.Items.Add(lvi);
                }
            }
        }

lol so is all that code i got at the top of this post uv done in 7. hehehehe

I`ve done in 7 what? :)

lines of code. altho it doesnt quite do what i want it to. the principle is there and i think it would only take a bit of changing
the code iv got now that does do its job is:

int collum = 0;
            int arraySize = 0;
            double cTotal = 0;
            // Read codes.txt in to codeNew
            FileStream codeNew = File.Open("C:\\codes.txt", FileMode.Open, FileAccess.Read);
            StreamReader codeRead = new StreamReader(codeNew);
            string codeView = codeRead.ReadToEnd();

            codeRead.Close();
            codeNew.Close();

            String[] codeArray = codeView.Split(new char[] { '*' });
            do
            {
                string code = codeArray[collum];
                ListViewItem item = new ListViewItem(code);//sets 1st item of code 
                code = codeArray[collum + 1];
                item.SubItems.Add(code);                   //sets 2nd item of code
                code = codeArray[collum + 2];
                item.SubItems.Add(code);                   //sets 3rd item of code
                cTotal = cTotal + double.Parse(code);
                listView1.Items.Add(item);
                collum = collum + 3;
                arraySize = arraySize + 3;
            } while (arraySize < (codeArray.Length));
            txtTCost.Text = cTotal.ToString();
        }

and you wrote:

private void ReadingFromFile()
   {                        
      using (StreamReader sr = new StreamReader(@"C:\2\testFile.txt"))            
      { 
         while (!sr.EndOfStream)
             {
              string line = sr.ReadLine();
              string[] array = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
              ListViewItem lvi = new ListViewItem(array[0].ToString());
              lvi.SubItems.Add(array[1]);
              this.listView1.Items.Add(lvi);
              }
          }
      }

which pretty much does the same thing. just doesnt add the third collum.

all i did to make it work is copy line 10, change [1] to [2] and put it between 10 and 11. change file name and the comma deliminater :) works a treet. much better :D

Great, I am glad you put it into work (code I mean).
If there is anything else you would like to know, justask, I`ll e glad to help you out.

how to write a simple C3 switch statement

int caseSwitch = 1;
switch (caseSwitch)
{
    case 1:
        Console.WriteLine("Case 1");
        break;
    case 2:
        Console.WriteLine("Case 2");
        break;
    default:
        Console.WriteLine("Default case");
        break;
}

maybe?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.