VB.NET的GridView忽略页脚总空值 [英] VB.NET GridView Ignore Null Values in Footer Total

查看:183
本文介绍了VB.NET的GridView忽略页脚总空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.net

Protected Sub monthlyReportsUK_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
   Dim rowTotal As Decimal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "webShopTotal").ToString)
   grdTotal = grdTotal + rowTotal
End If

If e.Row.RowType = DataControlRowType.Footer Then
   Dim lbl As Label = DirectCast(e.Row.FindControl("lblwebsVal"), Label)
   lbl.Text = "£" + grdTotal.ToString("##,0.00")
End If

End Sub

HTML

<asp:TemplateField HeaderText="webShopTotal">
<ItemTemplate>
    <asp:Label ID="lblamount" runat="Server" Text='<%# Eval("webShopTotal") %>' />
</ItemTemplate>
<FooterTemplate>
    <asp:Label ID="lblwebsVal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>

GridView控件

GridView

Month   Value
Jan     NULL
Feb     NULL
Mar     15.00
Apr     10.00

我希望能够总结的值:

I want to be able to sum the values:

   Month   Value
    Jan     NULL
    Feb     NULL
    Mar     15.00
    Apr     10.00
   Total    25.00

不过,我得到错误对象不能从DBNull的转换为其他类型。

有没有什么办法可以在页脚加总忽略NULL值?

Is there any way I can add the total in the footer ignoring NULL values?

推荐答案

试试这个

Protected Sub monthlyReportsUK_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then    
 If DataBinder.Eval(e.Row.DataItem, "webShopTotal").ToString IsNot Nothing AndAlso DataBinder.Eval(e.Row.DataItem, "webShopTotal").ToString IsNot DbNull.Value Then
   Dim rowTotal As Decimal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "webShopTotal").ToString)
   grdTotal = grdTotal + rowTotal
 End Id
End If

If e.Row.RowType = DataControlRowType.Footer Then
   Dim lbl As Label = DirectCast(e.Row.FindControl("lblwebsVal"), Label)
   lbl.Text = "£" + grdTotal.ToString("##,0.00")
End If

End Sub

修改

首先,你应该总是使用 System.Decimal.TryParse( )代替Convert.ToDecimal()

First, you should always use System.Decimal.TryParse() instead of Convert.ToDecimal()

Dim result  = 0
Dim rowTotal As Decimal = Decimal.TryParse(DataBinder.Eval(e.Row.DataItem, "webShopTotal").ToString), out result)

这篇关于VB.NET的GridView忽略页脚总空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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