Click here to Skip to main content
15,796,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting my datagrid field values in txt file through given below code. While exporting its working fine, only in formatting of datetime field, its have some issue.
In the datagrid fields, datetime formatting is "dd/MM/yyyy hh:mm:ss AM/PM", but after exporting in text file, its changed with "dd/MM/yyyy hh:mm:ss". "AM/ PM " value is missing in exported txt file.

Please suggest, how to keep required datagrid datetime format in exported txt file also.

please refer given code which i am using in my export option.

TextWriter sw = new StreamWriter(@"C:\\Export\\Export.txt");
int rowcount = dataGridView1.Rows.Count;
for (int i = 0; i < rowcount - 1; i++)
{
sw.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "|"
+ dataGridView1.Rows[i].Cells[1].Value.ToString() + "|"
+ dataGridView1.Rows[i].Cells[2].Value.ToString() + "|"
+ dataGridView1.Rows[i].Cells[3].Value.ToString() + "|"
+ dataGridView1.Rows[i].Cells[4].Value.ToString() + "|");



}
sw.Close();
MessageBox.Show("Data Exported Sucessfully");
Posted

1 solution

If you use the standard ToString implementation (i.e. with no parameters) on a DateTime object, then the string you get depends on the local settings for the PC it is being run on.
You need to cast the value to a DateTime (because the Value property returns an object) and call ToString with an appropriate format string: Formatting a DateTime for display - format string description[^]
 
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