获取所有值的总和在ListView的特定列在VB.Net [英] Get the sum of all values in a specific column of a ListView in VB.Net

查看:632
本文介绍了获取所有值的总和在ListView的特定列在VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让所有当前值的总和在ListView控件的第2列。
我不断收到一个空值例外:

  Me.ListView1.GetItemAt(ListView1.FocusedItem.Index,2)。文本)

和我也试过这仍然引发异常:

 昏暗×如双= CDbl(Form1.ListView1.Items.Item(指数).SubItems(2)。文本)

这是给我异常的code块是:

 公用Sub getSubtotal()
     昏暗指数为整数
     昏暗总价值为双     对于指数= 1到Form1.ListView1.Items.Count - 1
         昏暗×如双= CDbl(Form1.ListView1.Items.Item(指数).SubItems(2)。文本)
         总价值=总价值+ X
     下一个     MSGBOX(总价值)


解决方案

要记住用的ListView ,是子项(A件事0) ListViewItem的,所以子项(1)将引用的第一个实际分项。所以,你的指数为2实际上指的是什么出现在第三次栏。

您定义总价值一子,这使得它的局部变量里面 - 它只是驻留在那里。假设总还有些兴趣,这个过程应该是返回该值的函数。此外,你的循环跳过的第一个项目,就是有点罗嗦,而且存在 Form1上。您没有使用明确的形式引用指示(使用来引用当前表单实例):

 '可选TODO:通过指示哪些列一个int值加起来
 公共职能GetSubTotal()为十进制     昏暗总价值为十进制
     昏暗TMP为十进制     数组和集合索引(0)未开始(1)
     OP code将跳过第一个项目
     对于n为整数= 0〜ListView1.Items.Count - 1         'TODO:并非所有项目都必须有相同数量的子项
         还应该检查子项计数> = 1为每个项目
         试图获得的价值:
         如果Decimal.TryParse(ListView1.Items(N).SubItems(1)。文本,TMP)然后
              总价值+ TMP =
         万一
     下一个     总价值回归
 结束功能

I am trying to get the sum of all the current values in column 2 of a "ListView". I keep getting a "null value" exception for :

Me.ListView1.GetItemAt(ListView1.FocusedItem.Index, 2).Text)

and I also tried this which still threw the exception:

Dim X As Double = CDbl(Form1.ListView1.Items.Item(Index).SubItems(2).Text)

The Code block that is giving me the exception is:

  Public Sub getSubtotal()
     Dim Index As Integer
     Dim TotalValue As Double

     For Index = 1 To Form1.ListView1.Items.Count - 1
         Dim X As Double = CDbl(Form1.ListView1.Items.Item(Index).SubItems(2).Text)
         TotalValue = TotalValue + X
     Next

     MsgBox(TotalValue)

解决方案

A thing to keep in mind with the ListView, is that SubItem(0) refers to the ListViewItem, so SubItem(1) will reference the first actual subitem. So your index of 2 is actually referring to what appears in the third "column".

You define TotalValue inside a Sub which makes it a local variable - it only resides there. Assuming the Total is of some interest, the procedure should be a Function returning that value. Also, your loop skips the first item, is a little wordy, and the existence of Form1. indicates you are not using explicit form references (use Me to reference the current form instance):

 ' Optional ToDo: pass a int value indicating which "column" to add up
 Public Function GetSubTotal() As Decimal

     Dim TotalValue As Decimal
     Dim tmp As Decimal

     ' arrays and collections start at index(0) not (1)
     ' OP code would skip the first item 
     For n as Integer = 0 To ListView1.Items.Count - 1

         ' ToDo: Not all items must have the same number of SubItems
         ' should also check SubItems Count >= 1 for each item
         ' try to get the value:
         If Decimal.TryParse(ListView1.Items(n).SubItems(1).Text, tmp) Then
              TotalValue += tmp
         End If          
     Next

     Return TotalValue
 End Function

这篇关于获取所有值的总和在ListView的特定列在VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆