如何计算网格视图中的金额总和。 [英] how to calculate sum of Amount in grid view.

查看:88
本文介绍了如何计算网格视图中的金额总和。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码作为网格视图列的总和。

  Dim  a,b, c 作为 十进制 
对于 i = 0 GridItem.Rows.Count
a = Convert.ToDecimal(GridItem.Rows( i)。细胞( 7 )。Value.ToString())
c = c + a
下一步

txtBasic.Text = c



系统抛出错误:对象引用未设置为对象的实例。<调试代码后我知道了问题。我在网格视图中添加了2行。

问题是系统显示Gridview1.Rows.Count = 3.

为循环迭代计数3次第3次系统抛出错误:对象引用未设置为对象实例。



第一次我enter = 500.2nd time = 2000.at for循环值c = 2500的第二次迭代。

解决方案

每当使用对象层次结构时,应始终执行在试图向下钻取之前存在检查。这样你就可以消除你在这里遇到的问题。

例如。

  Dim  a,b,c  As   Decimal  
对于 i = 0 GridItem.Rows.Count
< span class =code-keyword> If !IsNothing(GridItem.Rows(i))!IsNothing(GridItem.Rows(i).Cells ( 7 然后
如果 十进制 .TryParse(GridItem.Rows(i).Cells( 7 )。值,出a) 然后
c = c + a
结束 如果
结束 如果
txtBa sic.Text = c.ToString()


这里给出了关于你的问题的整个解决方案:

http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer .html [ ^ ]


 对于 i =  0    GridItem.Rows.Count  -   1  


I am using following code for sum of grid view column.

Dim a, b, c As Decimal
        For i = 0 To GridItem.Rows.Count
            a = Convert.ToDecimal(GridItem.Rows(i).Cells(7).Value.ToString())
            c = c + a
        Next
        
        txtBasic.Text = c


System throws an error:Object reference not set to an instance of an object.
after debug the code I understand the problem.I add 2 rows in grid view.
Problem is system shows Gridview1.Rows.Count=3.
as per count for loop iterate 3 times 3rd time system throws an error:Object reference not set to an instance of an object.

1st time I entered =500.2nd time=2000.at second iteration of for loop value of c=2500.

解决方案

Whenever you work with an object hierarchy, you should always perform an existence check before attempting to drill down. That way you eliminate issues like what you are encountering here.
For example.

Dim a, b, c As Decimal
For i = 0 to GridItem.Rows.Count
If !IsNothing(GridItem.Rows(i)) And !IsNothing(GridItem.Rows(i).Cells(7) Then
   If Decimal.TryParse( GridItem.Rows(i).Cells(7).Value, out a ) Then
      c = c + a
   End If
End If
txtBasic.Text = c.ToString()


Entire solution is given over here regarding your problem :
http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html[^]


For i = 0 To GridItem.Rows.Count - 1


这篇关于如何计算网格视图中的金额总和。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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