Click here to Skip to main content
16,014,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting my datagrid value in text file. And my system date format is "MM/dd/yyyy". Here in this code, Cell1 and Cell2 has date time value. At the screen, it's visible as "dd/MM/yyyy" format but while exporting it converts in my system format as "MM/dd/yyyy".

Please suggest or provide way to handle this conversion case, where system date time format is MM/dd/yyyy.

============================================
C#
void Export_Grid()
        {
            TextWriter sw = new StreamWriter(@"F:\\Attendance2013\\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() + "|"
                + dataGridView1.Rows[i].Cells[5].Value.ToString() + "");

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

        }
Posted
Comments
virusstorm 1-Jul-15 10:21am    
I'm not sure I follow what your issue is. As long as the value can be parsed by DateTime, all you need to do is format it to the desired style.

You can use like this...

String.Format("{0:MM/dd/yyyy}", GridView1.Rows[i].Cells[1].Value)
 
Share this answer
 
When you use the default ToString implementation on a DateTime value, it is formatted according to the current system settings. So since your PC is set to MM/dd/yyyy, that is the format of the date as a string that is generated. To avoid that, use a format strign with the ToString:
C#
dataGridView1.Rows[i].Cells[n].Value.ToString("dd/MM/yyyy")


You might also want to consider using a StringBuilder and File.WriteAllText instead of a stream and string concatenation - it'll probably be a lot quicker and more efficient.
 
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