Hi there,
I have asked about this before, but cannot find
any "archived" articles on it. I want to format DateTime
values read from an Oracle DB in .NET to be shown on a
datagrid as MM/DD/YYYY. I can't get this to work. I have
used the Format(value, "MM/DD/YYYY") option, but this
doesn't work. Even when I try to fool the DataGrid into
thinking the column is a String type, as well as assigning
the value to a string variable, correctly formatted, the
thing still re-formats it to the long format. I am
starting to get very frustrated with it - it just won't do
what i tell it to. Far too clever by half, it is. Please
help with this! I attach a little of my code.
Thanks,
Paul.
'I need to get the date formatted - remove
the hours and minutes.
'dtRegDate = Format(dsc.Tables(0).Rows
(i).Item("REGISTERED_DATE"), "d")
'I response.wrote the altered values out, and confirm they
were correctlt adjusted
'Response.Write("<BR>" & dtRegDate)
'but when I applied the correctly formatted variables
back, they had 12.00.00 added.
'drComp.Item("REGISTERED_DATE") = dtRegDate
Yan-Hong Huang[MSFT] - 27 Feb 2004 08:45 GMT
Hi Paul,
Thanks for posting in the group.
Based on the description, the quesiton is: how to show datetime format as
MM/DD/YYYY in datagrids, right?
Could you please let me know which type of application you are working on
now, asp.net web form application or a windows form application?
For a web form application, you can set datetime format like:
In the property builder screen for the grid.
In the Columns tab.
In the select columns box, highlight your date field.
Then enter, {0:d/M/yyyy}, in the data formatting expression box.
There you go no more time in your date column.
you can also try:
Modify the format of the date while the data is being bound to your control.
private void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
// Get the data item and make sure it is a non-null object
DataRowView drv = (DataRowView) e.Item.DataItem;
if (drv == null)
return;
DateTime dt = Convert.ToDateTime(drv["MyDate"].ToString());
e.Item.Cells[yourCellIndex].Text = dt.ToString("MM/dd/yyyy"); //You
can try out different formats here.
}
If it is a windows forms applicaiton, you need to create a column style.
Column style is an object that defines what the column looks and behaves
like, including such things as color, font, and the presence of controls,
such as check boxes. The .NET Framework includes two types of column-style
classes by default: the DataGridTextBoxColumn and DataGridBoolColumn
classes. In this case, you need to customize a style class by inheriting
the class DataGridTextBoxColumn.
Hope that helps.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Yan-Hong Huang[MSFT] - 02 Mar 2004 01:11 GMT
Hi Paul,
How is everything going? Have you successfully show that date format in
datagrids? If there is anything unclear, please feel free to post here with
detailed information. I am glad to be of assistance.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
DotNetJunkies User - 26 Oct 2004 05:32 GMT
if the date field in the database is null, the datagrid will always display "1/1/1753", I want it to display an empty string or display nothing, can you help?
---
Yan-Hong Huang[MSFT] - 27 Oct 2004 03:13 GMT
Really?
Generally speaking, SqlDateTime must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM.
Have you checked you assign some default value to it? It should be
System.Data.SqlTypes.SqlDateTime.Null if the date is NULL.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn
This posting is provided "AS IS" with no warranties, and confers no rights.
DotNetJunkies User - 26 Oct 2004 22:00 GMT