views:

69

answers:

1

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

+1  A: 

very simple

<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" />
Muhammad Akhtar