Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15507

[RESOLVED] handle null cells when exporting a DGV

$
0
0
Hello again,
Issue # 2 with the application I am working on.
If all cells have data then everything works well, however if a cell is empty the program crashes and reports an error.

Any ideas?

Name:  2021-03-16_9-22-02.jpg
Views: 36
Size:  23.8 KB

Code:

Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles BtnExport.Click
        'Build the CSV file data as a Comma separated string.
        Dim csv As String = String.Empty


        'Add the Header row for CSV file.
        For Each column As DataGridViewColumn In DataGridViewMWD.Columns
            csv += column.HeaderText & ","c
        Next

        'Add new line.
        csv += vbCr & vbLf

        'Adding the Rows
        For Each row As DataGridViewRow In DataGridViewMWD.Rows
            For Each cell As DataGridViewCell In row.Cells
                'Add the Data rows.
                csv += cell.Value.ToString().Replace(",", ";") & ","c
            Next

            'Add new line.
            csv += vbCr & vbLf
        Next

        'Exporting to Excel
        If My.Computer.FileSystem.DirectoryExists("C:\SD\CalcExport\") Then
            Dim folderPath As String = "C:\SD\CalcExport\"
            File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)
            Process.Start("C:\SD\CalcExport\")

        Else My.Computer.FileSystem.CreateDirectory("C:\SD\CalcExport\")
            Dim folderPath As String = "C:\SD\CalcExport\"
            File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)
            Process.Start("C:\SD\CalcExport\")
        End If

    End Sub

Attached Images
 

Viewing all articles
Browse latest Browse all 15507

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>