0

I have diffrent entries in a ListView. I would like to count the entries (starting from the second column). The output should be under the "Total" column (see figure below, framed in red) How can this be achieved? With this code I can only access the individual rows:

for (int j = 1; j < listView1.Items[1].SubItems.Count; j++) { string s = listView1.Items[1].SubItems[j].Text; } 

enter image description here

Many thanks for your help!

4
  • Hi mike and welcome, when you say "(starting from the second column)", did you mean the second row as it looks like from the context ? and you want the sum of all the values or the count of the total column values from listview? Commented Dec 20, 2020 at 10:07
  • Thank you for pointing that out " Frankenstine Joe". You are right. I want to count the total column values. For example, there are 3 entries in the first row (A,B,C). The number of entries is 3. Or the 2 entries in the second row (P,B). The number of entries is 2 and so on. Commented Dec 20, 2020 at 10:43
  • I would like to count the entries Exisiting SubItems or filled ones? Filled with what? Note the LC.Items are a jagged array! Commented Dec 20, 2020 at 10:56
  • Very clever questions "TaW". I receive the entries in runtime. In principle filled ones as you noted. All entries are strings. Sometimes there a two entries in the first row or none. Sometimes there are three entries in the third row and so on. Commented Dec 20, 2020 at 11:47

1 Answer 1

1

Here's your solution

 //iterate through all rows for (int i = 0; i < listView1.Items.Count; i++) { //make sure that 5 subitems are present int missingSubitemsCount = 5 - listView1.Items[i].SubItems.Count; for (int j = 0; j < missingSubitemsCount; j++) listView1.Items[i].SubItems.Add(new ListViewItem.ListViewSubItem()); int count = 0; //test columns 1-3 for (int j = 1; j < listView1.Items[i].SubItems.Count - 1; j++) { //check if empty if (String.IsNullOrEmpty(listView1.Items[i].SubItems[j].Text) == false) count++; } //update last column listView1.Items[i].SubItems[4].Text = count.ToString(); } 

However, i think it would be better to use DataGridView and data binding instead. Operating directly on control can cause problems when project gets bigger. Its just better to separate data from view

Here's how i would do it with DataGridView

 class MyItem { public string Name { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public string Property3 { get; set; } public int Total { get; set; } } private void button2_Click(object sender, EventArgs e) { var items = new List<MyItem>(); items.Add(new MyItem { Name = "Books", Property1 = "A", Property2 = "B", Property3 = "C" }); items.Add(new MyItem { Name = "Pencils", Property1 = "A", Property2 = "B", }); items.Add(new MyItem { Name = "Tapes", Property1 = "A", }); //iterate through all items foreach (var item in items) { //get list of MyItem properties starting with "Property" foreach (var property in typeof(MyItem).GetProperties().Where(u => u.Name.StartsWith("Property"))) { //test if null or empty, increment Total if not if (property.GetValue(item) != null && String.IsNullOrEmpty(property.GetValue(item).ToString()) == false) item.Total++; } //Alternative: Same calculation in one line item.Total = typeof(MyItem).GetProperties().Count(u => u.Name.StartsWith("Property") && u.GetValue(item) != null && String.IsNullOrEmpty(u.GetValue(item).ToString()) == false); } //bind items to DataGridView dataGridView1.DataSource = items; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I appreciate it. Unfortunately i get an error message (OutofRange Exception). This concerns only the last line in your code: „ listView1.Items[i]. SubItems[4].Text = count.ToString()“. Please review the respective notes, that i have given to Mr. TaW.
Yes, this is exactly problem with working on control instead of data model i told you about. You dont have all subitems needed (i assume user fills ListView?). Therefore trying to write Subitems[4] raises exception cause you have only 3 or even less. I have edited my original answer, there is a check for subitems count and we add new if needed
Thank you so much for your efforts!! And this was true and everthing is working again! Thank you very much Rafal W !!
Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers, and by accepting the most helpful answer to any question you ask.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.