I have selected a bunch of data from my access database which one of the fields is a DateTime field.
I'm trying to show it formated in a GridView but when I try:
dtlJob.DataSource = genericDataSet
dtlJob.Fields(2).DataFormatString = "{0:d}"
dtlJob.DataBind()
I get this error on line 2
Error 2 'DataFormatString' is not a member of 'System.Web.UI.WebControls.DataControlField'.
How do I format my data?
EDIT
This is my DetailsGridView I'm trying t o show off
<asp:DetailsView ID="dtlJob" runat="server" Height="50px" Width="125px">
</开发者_如何学JAVAasp:DetailsView>
it has nothing but its tags for I'm fetching every data by code from a database. But I want to format the Data
Date Field which is appearing like this no more which data it has
You have to create a BoundField like the code below.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:BoundField DataField="Birth" HeaderText="Birth" DataFormatString="{0:d}" />
</Fields>
</asp:DetailsView>
I hope that helped.
Vicente
The fields property is readonly, so you can't modify it except in constructor. Please notice that I have not tested this solution, bt this is what I think you have to do.
So you have to create a new class that inherits from details view and shadows the Fields property, using its own private _fields field:
Imports System.Web.UI.WebControls
Public Class CustomDetailsView
Inherits DetailsView
Private _fields As System.Web.UI.WebControls.DataControlFieldCollection
Public Shadows Property Fields As System.Web.UI.WebControls.DataControlFieldCollection
Get
Return _fields
End Get
Set(ByVal value As System.Web.UI.WebControls.DataControlFieldCollection)
_fields = value
End Set
End Property
End Class
Then you have the following code, creating a CustomDetailsView object, telling it that its datasource is the dataset, formatting the second column field, and giving all hese information to your detaisview on the web form, before binding the data.
Dim myDetailsView = New CustomDetailsView
myDetailsView.DataSource = genericDataSet
CType(myDetailsView.Fields(1), BoundField).DataFormatString = "{0:d}"
dtlJob = myDetailsView
dtlJob.DataBind()
BoundField's base type is DataControlField, so you can write the CType line. I can't test this right now, please do it and post feedback. If it doesn't work, it could be a beginning of answer.
Greetings
Here is an another solution (still not tested). Here you still make a new class inheriting DetailsView, and you access the fields variable in the constructor:
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Public Class _CustomDetailsView
Inherits DetailsView
Public Sub New(ByVal Columns As List(Of String))
For Each item As String In Columns
Dim bfield As New BoundField
If Not String.IsNullOrWhiteSpace(item) Then
bfield.DataFormatString = item
Else
bfield.DataFormatString = ""
End If
Me.Fields.Add(bfield)
Next
End Sub
End Class
Then, you create a list of string containing the wanted formatting for each column. Then you create your _CustomDetailsView by passing this list of string in the constructor. Next you assign your DtlJob the _CustomDetailsView object, and finally you provide the datasource and proceed the databinding:
Dim DataFieldStyles = New List(Of String)
' First column: Default style
' Second column: Date format
' Third : Currency Format
' Fourth : Default style
DataFieldStyles.AddRange(New String() {"", "{0:d}", "{0:c}", ""})
Dim My_DetailsView As _CustomDetailsView = New _CustomDetailsView(DataFieldStyles)
dtlJob = My_DetailsView
dtlJob.DataSource = genericDataSet
dtlJob.DataBind()
Same remark as for my other answer, I can't test this, so try it and please give feedback.
Greetings
精彩评论