简体   繁体   中英

CSV Export from DataGrid - Wrong Rows

How can I export my datagrid to CSV correctly? Why are the Rows in different columns?

ExportToCsv function return:

Column Headers:

"SR #;8D Report Requested;Status (ASSIST);In R&D;BTQ NUMBER;Priority;Target Date;Implementation Date;Status (BTQ)"

Rows: WRONG!

"1-3271406718;yes;yes;BTQ00153254;6 - Enhancement;22.02.2014;09.09.2014 ;COMPLETED;Eng. wait"

How it should be:

Column Headers:

"SR #;8D Report Requested;Status (ASSIST);In R&D;BTQ NUMBER;Priority;Target Date;Implementation Date;Status (BTQ)"

Rows:

"1-3271406718;yes;Eng. wait;yes;BTQ00153254;6 - Enhancement;22.02.2014;09.09.2014 ;COMPLETED"

here my code:

                string CsvFpath = saveDLG.FileName;
                StreamWriter csvFileWriter = new StreamWriter(CsvFpath, false);
                string columnHeaderText = "";
                int countColumn = dgvView.Columns.Count - 1;
                if (countColumn >= 0)
                {
                    columnHeaderText = (dgvView.Columns[0].Header).ToString();
                }
                //Writing column headers
                for (int i = 1; i <= countColumn; i++)
                {
                    columnHeaderText = columnHeaderText + ';' + (dgvView.Columns[i].Header).ToString();
                }
                csvFileWriter.WriteLine(columnHeaderText);

                // Writing values row by row
                for (int i = 0; i <= dgvView.Items.Count - 2; i++)
                {
                    string dataFromGrid = "";
                    for (int j = 0; j <= dgvView.Columns.Count - 1; j++)
                    {
                        if (j == 0)
                        {
                            dataFromGrid = ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
                        }
                        else
                        {
                            dataFromGrid = dataFromGrid + ';' + ((DataRowView)dgvView.Items[i]).Row.ItemArray[j].ToString();
                        }
                    }
                    csvFileWriter.WriteLine(dataFromGrid);
                }
                csvFileWriter.Flush();
                csvFileWriter.Close();

Try it with Linq. It's simpler and easy to read:

    public string DataGridToCSV(string delimiter = ";")
    {
        var sb = new StringBuilder();

        var headers = myDataGridView.Columns.Cast<DataGridViewColumn>();
        sb.AppendLine(string.Join(delimiter, headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));

        foreach (DataGridViewRow row in myDataGridView.Rows)
        {
            var cells = row.Cells.Cast<DataGridViewCell>();
            sb.AppendLine(string.Join(delimiter, cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
        }
        return sb.ToString();
    }

Just save the String as *.csv File and you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM