tags:

views:

2755

answers:

6

Alright, I'm trying to read a comma delimited file and then put that into a ListView (or any grid, really). I have the delimiting part of the job taken care of, with the fields of the file being put into a multidimensional string array. The problem is trying to get it into the ListView.

It appears that there isn't a real way of adding columns or items dynamically, since each column and item needs to be manually declared. This poses a problem, because I need the ListView to be as large as the file is, who's size isn't set. It could be huge one time, and small another.

Any help with this would be appreciated.

+1  A: 

Just loop through each of the arrays in that you've created and create a new ListViewItem object (there is a constructor that takes an array of strings, I believe). The pass the ListViewItem to the ListView.Items.Add() method.

Jeffrey L Whitledge
Thanks, that worked. I just have to fix the SubItems in there, but that's exactly what I needed. Thanks!
Dropped.on.Japan
+1  A: 

You can load a csv file with ado.net and bind it to a datagrids data source. Or you could use linq for xml to parse the file and bind those results to a datagrid's data source property.

Aaron Fischer
A: 

In response to Jeffrey's answer.

I would do exactly that, but the problem that I'm running into is a basic one. How can I create these objects without naming them. Noobie question, but a problem for me, sadly. This is what I have so far.

int x = 0;
int y = 0;
while (y < linenum)
{
    while (x < width)
    {
        ListViewItem listViewItem1 = new ListViewItem(list[y,x]);
        x++;
    }
    y++;
    x = 0;
}

What should I do for the name of listViewItem1?

EDIT: I can't seem to get the formatting right for the code. Sorry for the unreadability.

Dropped.on.Japan
You can use the same variable to hold each of your list view items. Every time the "new ListViewItem(list[y,x])" is executed a new object is created. Then listView.Items.Add(listViewItem1) will add this new object to the control.
Jeffrey L Whitledge
+1  A: 

I would use the FileHelpers Library to read in the CSV file and then DataBind the collection to the ListView.

Use the DelimitedClassBuilder to dynamically create columns with the typeof(string) equal to the number of columns in your source file.

Load your CSV file into a DataTable using the RecordClass that you created and then set the ListView.DataSource to the DataTable.

Timothy Lee Russell
A: 

Linq To CSV

kenny
A: 

Is there a reason you can't use a DataTable? Use the DataSource member off of it.

Also, I hope you are using the String.Split function, and not manually parsing...

~S