简体   繁体   中英

Ugly time format in GridView ASP.NET C#

In my ASP.NET project I have a GridView to display times (among other things). I'm using Visual Studio 2012 and PostgreSQL and pgAdmin.

In the database times are stored in tbl_tid as:

在此处输入图片说明

This is also how they are displayed if I run the SQL in the database or print the times to a listbox.

BUT when I display the times in my GridView they come out like this: 在此处输入图片说明

So, how do I get rid of **0001-01-01 ** in each row (and where does it come from?).

In my code behind:

            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();

In my 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:

<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>

Why does time appear with "0001-01-01"?

The .NET framework is converting the PostGreSql time field into a Standard DateTime . Since there's no actual date involved, .NET is assuming the date associated with the time is DateTime.MinValue which is January 1, 0001.

How can you get rid of the date portion in your ASP.NET Grid?

You can format the DateTime so that the date portion is not displayed. .NET DateTime values are fairly easy to format, but you'll need to tell ASP.NET how to do that explicitly. One technique is to use a boundfield in your Grid like this:

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

You could also use a TemplateField like you did for your checkboxes. There are also plenty of Format Strings for DateTimes which you should be able to take advantage of.

I solved it using this code:

   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);
        }          
    }  

If you want to select time as a string, use to_char()

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

Demo: http://sqlfiddle.com/#!15/e3bcb/5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM