0
votes

So you have a csv-list in a textfile and a listview. Fields are

Group,Column1,Column2,Column3,Column4,Column5

Example(All strings)

"Beginning","1","Quiet","Some text","Some other text","c:\...\image.jpg"
"Beginning","2","Quiet","Some text2","Some other text2","c:\...\image2.jpg"
"Middle","3","Quiet","Some text3","Some other text3","c:\...\image3.jpg"
"Middle","4","Running","Some text4","Some other text4","c:\...\image4.jpg"

Group is to be a listviewgroup. Column1 a listviewitem, Column2 the 1st subitem of Column1, Column3 the 2nd subitem of Column1, Column4 the 3rd subitem of Column1, Column5 the last subitem of Column1.

Group and Column2 are not unique, all other columns are unique.

What would be the most efficient way to

  1. read a csv-file list with that structure
  2. populate the listview
  3. write to the csv-file

What I tried:

ListViewGroup k = new ListViewGroup("group1");
ListViewGroup l = new ListViewGroup("group2");
ListViewGroup a = new ListViewGroup("group3");
listView1.Groups.AddRange(new ListViewGroup[] {a, k, l });
ListViewItem item1 = new ListViewItem("column1_1", 0);
item1.SubItems.Add("column2_1");
item1.SubItems.Add("column3_1");
item1.SubItems.Add("column4_1");
item1.SubItems.Add("column5_1");
item1.Group=k;
listView1.Items.Add(item1);

ListViewItem item2 = new ListViewItem("column1_2", 0);
...

As a basis. But this method makes automation hard. Especially when there are more groups being reused. So I am looking for a way this is normally done.

2
Have you tried anything yet? Please post it here. - Abe Miessler
Could you post a few lines of the CSV file? - Abe Miessler
Does it have to be a Listview that you read the values into..? can you read them into a DataGrid..? - MethodMan
What would be the advantage over a listview? - Peter Fren
Depends on what you want to do once you load the listview.. if you want to view the data in a Table / Grid format then DataGrid would work. but I am not sure what you are wanting to do with the data once you populate the ListView - MethodMan

2 Answers

0
votes

Here is something that you can try for starters please report back if this will work for you. I would start with a single ListView First so that you get the gist of how things work.

FileStream fileStreamNew = File.Open("C:\\myCSVFile.csv", FileMode.Open, FileAccess.Read);
StreamReader streamRead = new StreamReader(fileStreamNew);
string strView = streamRead.ReadToEnd();
streamRead.Close();
fileStreamNew.Close();

String[] strArray = strView.Split(new char[] { ',' });
foreach (string strValue in strArray)
{
   listView1.Items.Add(strValue);
}
0
votes

In terms of importing and exporting to .csv (or any delimited file), there is a great class called TextFieldParser in (somewhat surprisingly) the Microsoft.VisualBasic namespace. You just point it to the file, say what the delimiters are and you get a string[] returned when you call ReadLine(). You can also define comment tags that make sure you're only importing the data you need.

The great thing is the exception handling - MalformedExceptions will be thrown if the line is of an unexpected format.

Have a look here for more details.

I've used it loads of times and it's never let me down! Also makes your code much easier to read.