Get the total column value of list view in c#
I need to get the total of the item prices(which are in the same column in a list view in C#) without selecting the label called lblTotal.The item prices come from the databases and need to get the sum of them within the application itself. Can you please help me and send your ideas.
I have build this using WinForms and I have designed the following code
float lblTotal = 0F;
for (int i = 0; i < orderList.Items.Count; i++) 开发者_开发知识库 { if (orderList.Items[i].Selected) { lblTotal = float.Parse(orderList.Items[i].SubItems[1].Text); } }However this is not working for my application. Thank you
I windows forms c# the following code will total up all the integer values in column 2, change the 2 to whatever column your values are stored in.
int column = 0; column = 2;
int total = 0; total = 0;
ListViewItem item = default(ListViewItem); foreach ( item in this.ListView1.Items) { total += int.Parse((string)item.SubItems(2).Text); }
It is best to somehow calculate the sum either by moving the calculation to the database (SUM(x)) or your business logic/data source on the client, instead of going through all the hoops of reading it from the UI and conversion/casting.
精彩评论