绑定到数据源时从gridview单元中删除零

时间:2014-08-28 14:23:04

标签: asp.net vb.net gridview

我有一个使用SQL Server 2012中的存储过程填充的gridview。我使用的是VS 2013 VB.net。

以下代码在页面加载上运行,使用类

创建gridview
Dim mReport As New MorningReport

Dim mReports As New List(Of MorningReport)
mReports = mReport.morningReportGridview

GridMorningReport.DataSource = mReports
GridMorningReport.DataBind()

在aspx页面中,我使用带有AutoGenerateColumns="false"

的模板字段

我有四列,最后一列有几个零(见下文),这些值总是零。

enter image description here

我想用零替换零,所以我尝试使用在上面的数据绑定之后触发的以下内容。

Public Sub removeZeros()

    Dim row As GridViewRow

    For Each row In GridMorningReport.Rows
        If row.Cells(3).Text = "0" Then
            row.Cells(3).Text = ""
        End If
    Next

End Sub

这没有任何作用,但是如果我将0更改为""和""至" 7"然后所有细胞都变为7

1 个答案:

答案 0 :(得分:1)

我对它进行了分类,顿悟了!!

每个单元格内部都有一个标签,所以我使用了FindControl

 Public Sub removeZeros()

    Dim row As GridViewRow

    For Each row In GridMorningReport.Rows
        If CType(row.Cells(3).FindControl("lblThermsday"), Label).Text = "0" Then
            CType(row.Cells(3).FindControl("lblThermsday"), Label).Text = ""
        End If
    Next

End Sub