Click here to Skip to main content
15,440,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to export from DataGridView to CSV with date format yyyy-mm-dd.

My current export file looks like:
1,24,custodie,8888888,Marius,8940012004412026020,01/01/1900 00:00:00,IN CUSTODIE,01/01/1900 00:00:00,,,,,DISTRIBUITOR,,04/06/2021 00:00:00,,

and i need to look like:

1,24,custodie,8888888,Marius,8940012004412026020,01-01-1900,IN CUSTODIE,01-01-1900 ,,,,,DISTRIBUITOR,,04-06-2021,,

now when i export it takes the date format from windows which is dd / mm / yyyy but i need it to be yyyy-mm-dd without changing the format in windows

this are the cells that need to be formated
writer.Write(rap_cuiclient.Rows[i].Cells[6].Value.ToString() + ",");
writer.Write(rap_cuiclient.Rows[i].Cells[8].Value.ToString() + ",");
writer.Write(rap_cuiclient.Rows[i].Cells[15].Value.ToString() + ",");


What I have tried:

private void ExportGridToCSV1()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "CSV (*.csv)|*.csv";
            sfd.FileName = "Output.csv";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                TextWriter writer = new StreamWriter(sfd.FileName);


                for (int i = 0; i < rap_cuiclient.Rows.Count - 1; i++)
                {
                    writer.Write(rap_cuiclient.Rows[i].Cells[0].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[1].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[2].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[3].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[4].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[5].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[6].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[7].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[8].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[9].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[10].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[11].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[12].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[13].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[14].Value.ToString() + ",");
                    writer.Write(rap_cuiclient.Rows[i].Cells[15].Value.ToString() + ",");
                    
                    writer.Write(rap_cuiclient.Rows[i].Cells[16].Value.ToString() + ",");

                    writer.WriteLine("");
                }
                writer.Close();
                MessageBox.Show("Date exportate");
            }
        }
Posted
Updated 23-Aug-21 19:49pm

1 solution

 
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