converting a DetailsView TemplateField cell value to short date string
I开发者_StackOverflow中文版 have a detailsView whose date values in a cell are currently being displayed in longDateFormat, i want to convert all date values in this DetailsView to short date.
For example, instead of 6/1/2010 12:00:00 AM, i want to display just 6/1/2010
For a Gridview, i can achieve that by the code blow
Protected Sub DetailsView4_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView4.DataBound
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim cellDate As Date
If Date.TryParse(e.Row.Cells(i).Text, cellDate) Then
e.Row.Cells(i).Text = String.Format("{0:d}", cellDate)
End If
Next
End If
End Sub
How can achieve the same with a DetailsView?
It can be achieved simply, if it is in template filed..
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("tDate", "{0:MM/dd/yyyy}") %>'></asp:TextBox>
or if it is not template field then
<asp:BoundField DataField="tDate" HeaderText="tDate" SortExpression="tDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" />
精彩评论