0

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"));
2
  • Can you define "fails"? Also - perhaps check you have the array indexes the right way around... Commented May 21, 2009 at 20:44
  • In what way does it not work? I just tried it here and it works fine. Commented May 21, 2009 at 20:45

2 Answers 2

1

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>.

Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.