ASP.net从VB GridView隐藏日期时间

时间:2018-09-24 05:38:46

标签: asp.net vb.net

请记住我的英语,我正在从数据库中加载一些数据,并按如下所示绑定到asp gridview中。

<asp:TemplateField HeaderText="last_modified_date">
    <ItemTemplate>                           
        <asp:Label ID="lblModyDate" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase" Text='<%# Bind("last_modified_date") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

但是在我的数据库中,我有datetime列,该列的值为 1900-01-01 00:00:00.000 ,这意味着尚未设置该日期,所以我想要的是找到该日期,请勿将其显示在gridview中。

2 个答案:

答案 0 :(得分:0)

您可以在gridview加载事件的代码中使用以下条件。

if(labelmodydate.text == 1900-01-01 00:00:00.000)
{
labelmodydate.visible = false;
}

答案 1 :(得分:0)

由于您正在gridview的Label中使用ItemTemplate控件,因此可以执行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.000datetime列的标准表示形式,如果您在标记页中使用不同的字符串表示形式,则日期格式和值都应检查被调整,例如01/01/1900,格式为dd/MM/yyyy

相关问题:

Need to check whether date contains 1/1/1900

Hide 1900-01-01 date in GridView