Perfect Solution for Dates Not Displaying TDs in GridView

  • 2021-07-22 09:28:25
  • OfStack

Two processing methods:

1. Template column: Assume that the type of field completeTime in the data table is time format


 <asp:TemplateField HeaderText=" Time ">
            <ItemTemplate>
              <%#Eval("completeTime", "{0:yyyy-MM-dd}")%>
           </ItemTemplate>
   </asp:TemplateField>

2. Bind columns:


 <asp:BoundField HeaderText=" Time " DataField="completeTime" HtmlEncode= "false" DataFormatString="{0:yyyy-MM-dd}" >   
 </asp:BoundField>

DataFormatString use notes:

What is stored in the database is the date and time, but only the date is displayed in the page. datalist and gridview can be formatted with DataFormatString, so that the data can be displayed in the specified format.

Examples:


 <asp:DataList ID="dlistNews" runat="server" >
      <ItemTemplate>
     <a title=<%#DataBinder.Eval(Container.DataItem,"title")%> target="_blank" href='sysbin/news/news_show.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>'>
     <%#GetSubString(DataBinder.Eval(Container.DataItem,"title").ToString(),24)%></a>
    (<%# DataBinder.Eval(Container.DataItem,"showtime")%>)

<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:d}")%>  Or 

<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:yyyy-MM-dd}")%>
       </span>
      </ItemTemplate>
    </asp:DataList>

If you use bind () to take the value, also put the string in the specified format after it


<ItemTemplate> 
<asp:Label ID="Label1" runat="server" Text='<%# Bind("AddinTime", "{0:yyyy-mm-dd}") %>'></asp:Label> 
</ItemTemplate> 
</asp:TemplateField>

Control digit: DataFormatString = "{0: F}", which is the default format and displays two decimal places, or other decimal places if necessary

Value, DataFormatString = "{0: Fn}".

Specific usage:

DataFormatString= "{0: format string}"

The {0} in DataFormatString represents the data itself, and the format string after the colon represents the format in which you want the data to appear;

Number, currency format:

After the specified format symbol, you can specify the number of digits to display the decimal. For example, if the original data is "1.56", if the format is set to {0: N1}, enter

It is "1.5". Its common numerical format is shown in the following table:

Format string input result

"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68

Common date and time formats:

Format description output format

d Reduced Date Format MM/dd/yyyy

D Detail Date Format dddd, MMMM, dd, yyyy

f Full Format (long date + short time) dddd, MMMM dd, yyyy HH: mm

F

Full date-time format

(long date + long time)

dddd, MMMM dd, yyyy HH:mm:ss

g 1 Format (short date + short time) MM/dd/yyyy HH: mm

G 1 General Format (short date + long time) MM/dd/yyyy HH: mm: ss

m, M month-day format MMMM dd

s Moderate Date-Time Format yyyy-MM-dd HH: mm: ss

t Reduced Time Format HH: mm

T verbose time format HH: mm: ss

Note: If GridView is formatted with DataFormatString,

DataFormatString can only take effect if HtmlEncode = false is set at the same time.

< asp: BoundField HeaderText= "Total" DataField= "Total" DataFormatString= "{0: C}" HtmlEncode= "False" >


Related articles: