【问题标题】:ASP.net hide datetime from vb gridviewASP.net 从 vb gridview 隐藏日期时间
【发布时间】:2018-09-24 05:38:46
【问题描述】:

请注意我的英语,我正在从数据库中加载一些数据并绑定到 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>

但在我的数据库中,我的日期时间列具有此值 1900-01-01 00:00:00.000,这意味着该日期尚未设置,所以我想要的是如果找到此日期,不要在gridview中显示。

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    因为您在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.000datetime 列的标准表示形式,如果您在标记页面中使用不同的字符串表示形式,则应调整要检查的日期格式和值,例如01/01/1900dd/MM/yyyy 格式。

    相关问题:

    Need to check whether date contains 1/1/1900

    Hide 1900-01-01 date in GridView

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多