Jason
Jason

Reputation: 27

How can I export the datagridview to .csv without headers?

My csv file is like this There is no blank spaces or whatsoever.

The problem as you can see is that I do not know how to export my datagridview as .csv excluding column headers.

This is how I have done my export code:

    Private Sub IncomeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IncomeToolStripMenuItem.Click
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "CSV|*.csv"
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        Dim incomefile As String = String.Empty

        For Each column As DataGridViewColumn In Expense.Columns
            incomefile = incomefile & column.HeaderText & ","
        Next

        incomefile = incomefile.TrimEnd(",")
        incomefile = incomefile & vbCr & vbLf

        For Each row As DataGridViewRow In Expense.Rows
            For Each cell As DataGridViewCell In row.Cells
                incomefile = incomefile & cell.FormattedValue.replace(",", "") & ","
            Next
            incomefile = incomefile.TrimEnd(",")
            incomefile = incomefile & vbCr & vbLf
        Next
        System.IO.File.WriteAllText(saveFileDialog1.FileName, incomefile)
    End If

    Dim msg1 = "Export Successful"
    Dim title = "Excel Export"
    MsgBox(msg1, , title)
End Sub

Please advise me. Some others mentioned that I'd better off use datatable to export it, but since I started to learn computer programming 43 hours ago, I have no clue on how to declare the data i have put in my datagridview and export it as csv file.

Upvotes: 0

Views: 2202

Answers (2)

ASH
ASH

Reputation: 20302

Give this a try.

Private Sub BtnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExport.Click
        SFD.InitialDirectory = "C:\"
        SFD.Title = "Save Your File"
        SFD.Filter = "Microsoft Excel(*.xls)|*.xls|Comma Delimited File(*.csv)|*.Csv"
        SFD.OverwritePrompt = True
        SFD.ShowDialog()
        strFileName = SFD.FileName
        ' If SFD.ShowDialog() = DialogResult.OK Then
        If SFD.FilterIndex = 1 Then
            Call export()
        Else
            Call csv()
        End If
        ' End If

    End Sub

Also, try it this way.

Dim numCols As DataGridViewCell
        Dim sw As New System.IO.StreamWriter("d:\\output.txt")
        For Each numRows As DataGridViewRow In DataGridView1.Rows
            Dim intCellCount As Integer = numRows .Cells.Count
            Dim intCounter As Integer = 1
            For Each numCols  In numRows .Cells()
                If intCounter <> intCellCount Then
                    sw.Write(numCols .Value.ToString & ",")
                Else
                    sw.WriteLine(numCols .Value.ToString)
                End If
                intCounter += 1
            Next
        Next

Upvotes: 0

Chillzy
Chillzy

Reputation: 468

Remove those lines

For Each column As DataGridViewColumn In Expense.Columns
  incomefile = incomefile & column.HeaderText & ","
Next

Upvotes: 1

Related Questions