1

In C#, I have a ListView control and want to obtain a count of items currently in the control's item collection, but the code below produces the error:

'System.Windows.Forms.ListView.ListViewItemCollection' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Windows.Forms.ListView.ListViewItemCollection' could be found (are you missing a using directive or an assembly reference?)

ListView.ListViewItemCollection lvitems = lvDropSummary.Items;
int iLVItemsCount = lvitems.Count();

I haven't seen specific code examples anywhere getting the count of all items in a listview, but according to the documentation (and intellisense), the property exists:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.count(v=vs.100).aspx

1
  • do you have using System.Linq in your using statement /header section Commented Aug 1, 2013 at 16:11

2 Answers 2

5

you are trying to use like a method

int iLVItemsCount = lvitems.Count();

change it to the following should work

 int iLVItemsCount = lvitems.Count;
Sign up to request clarification or add additional context in comments.

Comments

2

Exactly what DJ KRAZE said. Just wanted to add, (not sure if you did it on purpose) but unless you are modifying the items collection before you do the count, you can simplify to:

int iLVItemsCount = lvDropSummary.Items.Count;

Comments

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.