【问题标题】:Ugly time format in GridView ASP.NET C#GridView ASP.NET C#中丑陋的时间格式
【发布时间】:2014-06-28 02:40:39
【问题描述】:

在我的 ASP.NET 项目中,我有一个 GridView 来显示时间(除其他外)。我正在使用 Visual Studio 2012 和 PostgreSQL 和 pgAdmin。

在数据库中时间存储在 tbl_tid 为:

如果我在数据库中运行 SQL 或将时间打印到列表框,这也是它们的显示方式。

但是当我在 GridView 中显示时间时,它们会像这样出现:

那么,我如何去除每一行中的 **0001-01-01 **(以及它来自哪里?)。

在我的代码后面:

            string sql = "SELECT tid FROM tbl_tid";
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);  
            ds.Reset();                 
            da.Fill(ds);                
            dt = ds.Tables[0];          
            GridView1.DataSource = dt;  
            GridView1.DataBind();

在我的 asp.net 中

                    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="OnRowDataBound" Height="118px" Width="390px">
                    <Columns>
                    <asp:TemplateField >
                         <HeaderTemplate>
                             <input id="chkAll" onclick="SelectAllCheckboxes(this);" runat="server" type="checkbox" />                                        
                         </HeaderTemplate>            
                         <ItemTemplate>
                             <asp:CheckBox ID="myCheckBox" runat="server" /> 
                         </ItemTemplate>
                    </asp:TemplateField>
                    </Columns>
                    </asp:GridView>

脚本:

<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'> </script>
<script>      
function SelectAllCheckboxes(chk) {
     $('#<%=GridView1.ClientID%>').find("input:checkbox").each(function () {
         if (this != chk) {
             this.checked = chk.checked;
         }
     });
}
</script>

【问题讨论】:

    标签: c# asp.net sql postgresql gridview


    【解决方案1】:

    为什么时间会出现“0001-01-01”?

    .NET 框架正在将 PostGreSql 时间字段转换为 Standard DateTime。由于不涉及实际日期,.NET 假设与时间相关的日期是DateTime.MinValue,即 0001 年 1 月 1 日。

    如何去除 ASP.NET 网格中的日期部分?

    您可以格式化 DateTime,以便不显示日期部分。 .NET DateTime 值的格式化相当容易,但您需要明确告诉 ASP.NET 如何进行格式化。一种技术是在你的 Grid 中使用一个 boundfield,如下所示:

    <asp:boundfield datafield="TheNameOfYourDateColumn" 
                    dataformatstring="{0:H:mm:ss}" 
                    htmlencode="false" />
    

    您也可以像在复选框中那样使用 TemplateField。还有很多Format Strings for DateTimes,您应该可以利用它们。

    【讨论】:

    • 谢谢,这可以正确格式化时间,但它会在 GridView 中添加一个额外的列。我自己解决了,看我的解决方案。
    【解决方案2】:

    我用这段代码解决了这个问题:

       protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int columnIndex = 0;
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    if (cell.ContainingField is BoundField)
                        if (((BoundField)cell.ContainingField).DataField.Equals("tid"))
                            break;
                    columnIndex++;
                }
                string columnValue = e.Row.Cells[columnIndex].Text;              
                string[] splitString = columnValue.Split(' ');
                string tid = splitString[1];
                e.Row.Cells[columnIndex].Text = tid.Remove(5,3);
            }          
        }  
    

    【讨论】:

      【解决方案3】:

      如果要选择时间作为字符串,使用 to_char()

      string sql = "SELECT to_char(tid, 'HH12:MI:SS') AS tid FROM tbl_tid";
      

      演示:http://sqlfiddle.com/#!15/e3bcb/5

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 2011-08-11
        • 2019-08-18
        • 1970-01-01
        • 1970-01-01
        • 2023-02-01
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        相关资源
        最近更新 更多