Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having trouble exporting my date from a DataGridView to excel without the date format flipping around if the months days are 12 or less.i.e date from 03/09/2013 changes to 09/03/2016. howerver date 20/03/2016 does not change to 03/20/2016.

VB.NET
oSheet.Range(EE).Value = DgvChequeData.Item("Maturity", RNO).Value



What could be the issue?

Thanks

What I have tried:

I have tried setting the format for the excel date but not working

oSheet.Range(EE).Value =FormatDateTime(Trim(DgvChequeData.Item("Maturity", RNO).Value), DateFormat.ShortDate)
Posted
Updated 2-Aug-16 17:19pm
v3

It's most likely due to the regional settings. Convert the source value to a DateTime object, and use the Value2 property on the Excel range.
C#
oSheet.Range(EE).Value2 = Convert.ToDateTime(DgvChequeData.Item("Maturity", RNO).Value)

How to: Programmatically Store and Retrieve Date Values in Excel Ranges[^]
 
Share this answer
 
Comments
Maciej Los 2-Aug-16 15:26pm    
5ed!
It should help as well. On the other side, the reason might be in NumberFormat of Excel cell.
You have to check the number format for Excel's cells.

Range.NumberFormat property (Microsoft.Office.Interop.Excel)[^]
Range.NumberFormat Property (Excel)[^]

For example:
VB.NET
'changes format of cell to store any data as a text.
oSheet.Range(EE).NumberFormat = "@"


For further details, please see: Custom Number Formats, Date & Time Formats in Excel & VBA; NumberFormat property [^]
 
Share this answer
 
Comments
pravin9455 2-Aug-16 16:16pm    
i want to keep the format as date
Maciej Los 2-Aug-16 16:19pm    
And...
oSheet.Range(EE).Value = DgvChequeData.Item("Maturity", RNO).Value
oSheet.Range(EE).NumberFormat = "m/d/yyyy"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900