因为您在gridview 的ItemTemplate 中使用Label 控件,所以您可以使用FindControl 来检查RowDataBound 事件中的标签值,如下例所示:
Protected Sub GridViewID_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridViewID.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblModyDate As Label = CType(e.Row.FindControl("lblModyDate"), Label)
' #1900-01-01 00:00:00.000# is just an example format
' change this depending on actual datetime representation
If (DataBinder.Eval(e.Row.DataItem, "last_modified_date").ToString().Contains("1900-01-01 00:00:00.000")) Then
' hide corresponding value
lblModyDate.Text = ""
End If
End If
End Sub
或者不使用任何代码隐藏事件,这次使用Eval() 而不是Bind():
<asp:TemplateField HeaderText="last_modified_date">
<ItemTemplate>
<asp:Label ID="lblModyDate" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase"
Text='<%# If(Convert.ToString(Eval("last_modified_date"), "{0:yyyy-MM-dd HH:mm:ss}") = "1900-01-01 00:00:00.000", "", Eval("last_modified_date", "{0:yyyy-MM-dd HH:mm:ss}")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
注意: 1900-01-01 00:00:00.000 是 datetime 列的标准表示形式,如果您在标记页面中使用不同的字符串表示形式,则应调整要检查的日期格式和值,例如01/01/1900 与 dd/MM/yyyy 格式。
相关问题:
Need to check whether date contains 1/1/1900
Hide 1900-01-01 date in GridView