views:

649

answers:

2

So, here's my question: Why won't the code in the first snippet work when the second one works fine. Also, I have set the view property to details. I've read all over how to add lvi's to the listview, and it fails every time... except for then I do it manually.

So, this doesn't work...

// Iterating through the rows...
for (int x = 0; x < numRows; x++) {
    row = new List<string>();
    // Iterating through the cols...
    for (int y = 0; y < numCols; y++) {
        row.Add(data[y][x]);
    }
    lv.Items.Add(new ListViewItem(row.ToArray()));
}

But this will work:

lv.Items.Add(new ListViewItem("foo"));
A: 

The ListViewItem is looking for a String[] try casting row.ToArray() to a String[].

hipplar
Assuming that row is typed as `List<string>` (per the init), then row.ToArray() is already a string[]
Marc Gravell
Since row is declared as List<string> ToArray is already coming as string[]; no need to cast.
Fredrik Mörk
@Fredrik - actually, the row declaration isn't shown... we might *assume* it is List<string>...
Marc Gravell
I assumed based on this code in the sample: row = new List<string>();
Fredrik Mörk
...but you are right of course, Marc ;)
Fredrik Mörk
I just ran the code from above with some quick test data and it worked just fine. What error are you seeing?
hipplar
+1  A: 

row.Add(data[y][x]) seems suspicious. Why do you access the data in column-first order but iterate in row-first order? Also, make sure the type of row (you didn't tell us this) is actually List<string>.

John Feminella