0

I have created a Form using the VS 2013 designer.
I added a Listview control and added 13 columns via the Properties window.
I then wrote a program in C# and I am able to access all of those columns using the item.SubItems[N] construct. Also, all of those column headers appear in the Listview on the form.

Later I added 2 new columns. The new columns show up in the Listview but throw an invalid index error when I try to access them. Also the SubItems.Count does not reflect the increase of columns in the collection. I also notice that, in the form.Designer.cs file, those new columns are not there.

What can I do to make sure the designer file gets updated and my new columns are added to the collection? (note: this is not related to an 'off-by-one' error or a mistake in indexing number) Thanks in advance.

3
  • 1
    This is really hard to read. Can you please format the question at least into a few paragraphs? Commented May 21, 2015 at 2:02
  • 2
    Welcome to Stack Overflow! Please review the help section of the site how to ask. As it stands, your question is valid question for the site but would be better received if you provided enough code for someone to replicate the problem you are having. Commented May 21, 2015 at 2:09
  • You just stopped too soon. Adding the columns is not enough enough, you also have to add the sub-items to the ListViewItems that you add to the ListView. Commented May 21, 2015 at 6:11

1 Answer 1

2

The reason for this behaviour is that the ListView Columns do not provide you with a regular matrix or grid like a Table or a DataGridView.

Instead they are what you would call a 'Jagged Array', meaning, that not all rows have the same number of columns.

So while you have provided / increased the maximum number of Columns the ListView will display, each Item has only as many SubItems as you have added.

If you add Columns at runtime you can display and access added SubItems, but only where you have actually added them to the Item. Before adding the extra SubItems they don't exist!

This is different from, say a DataGridView where you can freely assign values to each Cell, incuding those that belong to Columns you have created just in the previous line of code..

This is quite similar to the two types of Arrays, Jagged Arrays and Multidimensional Arrays..

But to get back at your title: The actual listView.Columns.Count has increased; what has not is the ListViewItem.SubItems.Count for any of the items.

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

3 Comments

Interesting and thanks for the response. In the code, before I access a SubItem, I usually add the item first with all the subitems explicitly included, ala: listView.Items.Add(new ListViewItem(new string[] { sub1, sub2, etc. } )); so I would have thought that I had added all the subitems. It seems rather random. That is, I can't determine when it will work and when it won't. Funny, because I thought I was asking an IDE related question rather than a coding question...but if what you're saying is true, just adding the column in the IDE should not solve the problem. Thanks again.
Just another followup to TaW. When I checked again, I realized I wasn't always initializing the full list of subitems as you suggested. My fault of course and you were 100% correct. Thanks very much for that excellent answer.
If you are happy with it, please consider marking it as accepted

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.