vinod chand

vinod chand

  • NA
  • 16
  • 10k

datetime formating in datagrid export in txt file in c#

Jun 19 2015 7:59 AM
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");

Answers (2)