John.P
John.P

Reputation: 657

Display TextFile to listView

I am trying to display an opened textfile in textboxes and listViews. Every line on the textfile represents a value.

Here is what I have done now:

public void OpenFile()
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
    string line = "";
    int index = 0;
    if (openFileDialog.ShowDialog() == true)
    using (StreamReader sr = File.OpenText(openFileDialog.FileName))
    {
        while ((line = sr.ReadLine()) != null)
        {
            index++;
            if (index == 1)
                InvoiceNumbertxt.Text = line;
            else if (index == 2)
                InvoiceDatetxt.Text = line;
            else if (index == 3)
                DueDatetxt.Text = line;
            else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9)
                PersonInfolst.Items.Add(line);
            else if (index == 10)
            {
                System.Windows.MessageBox.Show(index.ToString());
                Items.Add(new ItemProperties { 
                    Item = line, 
                    <<-- //Problem here. index is never 11.
                    Description = (index == 11) ? line : ""
                });
                itemlst.ItemsSource = Items;
            }
            else
                break;
        }
    }
}

index is a convenient flag (variable) to insert the lines in order, and not overlap multiple lines into the same control.

Everything seems to work except that I can't figure out how to add different columns to the same row on the listView.

Normally when a user is adding different columns to the same row, the user uses something like this:

 Items.Add(new ItemProperties { 
      Item= "3", 
      Description = "Strong Bear", 
      Publisher = "Blizzard" 
});

All column are inside the same ItemProperties property so that they all are included to the same row.

I want to to achieve the same thing but I need to check index for each value added. So far everything worked perfectly but I can't increase the index inside the else if statement to add multiple columns to the same row

Upvotes: 1

Views: 1025

Answers (1)

Alex
Alex

Reputation: 13224

Assuming that the file is required to contain at least those first 3 lines in order to be valid, what you can do is something that is easier:

public void OpenFile()
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
    if (openFileDialog.ShowDialog() && File.Exists(openFileDialog.FileName))
    {
        // Read all the lines from the file and store them in an array.
        var lines = File.ReadAllLines(openFileDialog.FileName);

        // Sanity check: there must be at least 3 lines.
        if (lines.Length < 3)
            System.Windows.MessageBox.Show("Error: not enough lines in file: " + openFileDialog.FileName);
        else
        {
            InvoiceNumbertxt.Text = lines[0];
            InvoiceDatetxt.Text = lines[1];
            DueDatetxt.Text = lines[2];
            foreach (var l in lines.Skip(3).Take(6))
                PersonInfolst.Items.Add(l);    

            // I am assuming that the `ItemProperties` follow, as groups of 3 lines,
            // consisting of `Item`, `Description`, `Publisher`
            for (var i = 9; i + 2 < lines.Length; i += 3)
            {
                Items.Add(new ItemProperties { 
                    Item = lines[i],
                    Description = lines[i + 1],
                    Publisher = lines[i + 2]
                });
            }
            itemlst.ItemsSource = Items;
        }
    }
}

Upvotes: 1

Related Questions