Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really Formatting a Datetime in a Bound Column

Status
Not open for further replies.

organicg

Programmer
Oct 21, 2002
151
US
I have a SmallDateTime field from SQL Server that is bound to a column in a Datagrid. It shows as MM/DD/YYYY HH:MM:SS AM/PM. I want to use the VB date format functionality to format it as "DayOfWeek, MM/DD/YYYY", but the BoundColumn tag doesn't seem to support this, it only supports a DataFormatString attribute that takes {0:c} syntax. Seems like there's gotta be a way to call a server side function so I can use VB formatting. I cannot figure out how to access a 'text' or 'value' property of the bound column either, so that's a problem. Can someone help me out with this? Thanks.
 
Hey, I figured out how to format it...just not in the format that I really want. In the Sub DataGrid1.ItemDataBound I do:
If isdate(e.Item.Cells(4).Text) Then
FormatDateTime(e.Item.Cells(4).Text, DateFormat.LongDate)
End If

I'd really like to format it to 'DayOfWeek, MM/DD/YY', but there's no custom format parameter. I guess I can parse the good stuff from two calls to this function and make it look the way I want. Anyone know if I can just set a custom format, like, 'dddd MM/DD/YY' some other way?
[yinyang]
 
Awright, figured it out. This seems like a pretty common task so I'll tell you how I did it. My first encounter with the benefits of Inheritance, I believe. The ItemDataBound event handler is where I catch the date field for each row being added to the grid. Get the value, set it to a Datetime variable, and then use the formatting functionality to set a custom format.

Private Sub DataGrid1_ItemDataBound
if IsDate(e.Item.Cells(4).Text) Then
dim dt as Datetime = CDate(e.item.cells(4).text)
e.Item.cells.(4).text = dt.ToString("dddd MM/dd")
End If
End Sub
[yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top