2

I am trying to achieve a situation where i load a file into the program. I can use streamreader for this. Each record will be 7 lines long. Therefore lines 1,8,15,22,etc will all hold the same value. As will 2,9,16,23, etc and so on.

What is the best way to achieve this? So that when i load the records in the listview, it recognises what i just said. Thanks

3
  • 1
    like a table? If 1,8,15 etc. all hold the exact same value, there's not much point to reading the whole thing... Commented Apr 19, 2010 at 13:47
  • @Luke: could you please give an example of input file, and what do you expect as output? Commented Apr 19, 2010 at 13:49
  • The input file: Name Address Number Email Delivery Instructions Status However, that will be for each customer, so you will end up with a long list in a text file where 1-7 is customer 1, 8-15 is customer 2, etc Commented Apr 19, 2010 at 14:18

3 Answers 3

3

When you say that lines 1, 8, 15 etc will all hold the same value, do you actually mean that they hold the same type of value? Otherwise, why read in more than the first 7 lines?

I think something like the below might work (not tested the code).

string data; using(System.IO.StreamReader reader = new System.IO.StreamReader("filepath")) { data = reader.ReadToEnd(); } string[] lines = data.Split(Environment.NewLine); for(int index = 0; index < lines.Length; index += 7) { ListViewItem item = new ListViewItem(); for(int innerIndex = 0; innerIndex < 7; innerIndex++) { item.SubItems.Add(lines[index + innerIndex]); } listView1.Items.Add(item); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, like i have records each lasting 7 lines. So say the first line is a customer name, the 8th will be the next customer line and so on. I need 1-7 as customer 1, 8-15 as customer 2, just think i may be overthinking.
I think that the above code (or something quite similar since it's untested) should be ok then.
1

Sounds like you need to create a class that will represent each record (one property for each of the 7 lines).

Open your stream reader.

Do, start looping while there are still lines to read.

For each iteration of your loop, read the 7 lines into seven variables, then create a new instance of your class and set each property to the value contained in the appropriate variable.

Add that class to a collection (List<MyClass>, for example).

Your ListView should use the collection of objects that you have constructed. You can now choose a property to act as the display value.

Comments

0

create a [num_lines/7] x [7] 2D array

for (int i=0;i<num_lines/7;i++) for (int t=0;t<7;t++) my2darray[i][t] = orig_array[i*7+t]; 

If i understand your question correctly, that will organize your data in the desired fashion

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.