Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Distributed Applications / October 2004

Tip: Looking for answers? Try searching our database.

DatTime formattingion datagrids

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul N - 26 Feb 2004 23:25 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.