none
vb.net - how to save a .csv without a savefiledialog RRS feed

  • Question

  • Hi,

    I monthly export a datagridview in csv file. how can I do it without using a savefiledialog?

    Thank you.

    Regards.

    Sunday, June 16, 2019 7:09 PM

Answers

  • Hello,

    The following will export a DataGridView where the first row is the column headers followed by each row of the DataGridView. If you don't want the column headers comment out the first sb.AppendLine.

    Add the following code module to your project.

    Imports System.IO
    Imports System.Runtime.CompilerServices
    Imports System.Text
    
    Public Module DataGridViewExtensions
        <Extension()>
        Public Sub ExportRows(sender As DataGridView, fileName As String, ByVal Optional delimiter As String = ",")
            If sender.RowCount > 0 Then
                Dim sb = New StringBuilder()
                Dim headers = sender.Columns.Cast(Of DataGridViewColumn)()
                sb.AppendLine(String.Join(delimiter, headers.Select(Function(column) column.HeaderText)))
    
                For Each row As DataGridViewRow In sender.Rows
    
                    If Not row.IsNewRow = True Then
                        Dim cells = row.Cells.Cast(Of DataGridViewCell)()
                        sb.AppendLine(String.Join(delimiter, cells.Select(Function(cell) cell.Value)))
                    End If
                Next
    
                File.WriteAllText(fileName, sb.ToString())
            End If
        End Sub
    End Module
    

    Simple usage, change the name of the DataGridView and change the path and file name to suit your needs.

    Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exported.csv")
    customersAccessDataGridView.ExportRows(fileName)
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by pb1990 Thursday, June 27, 2019 2:22 PM
    Sunday, June 16, 2019 8:51 PM
    Moderator

All replies

  • Hi,

    I monthly export a datagridview in csv file. how can I do it without using a savefiledialog?

    Thank you.

    Regards.

    Hi

    Here is one way:

    ' Form1 with DataGridView1 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' some test dummy data
    		With DataGridView1
    			.Columns.Add("One", "One")
    			.Columns.Add("Two", "Two")
    			.Columns.Add("DateDeCouvaison", "DateDeCouvaison")
    			.Columns.Add("Three", "Three")
    
    			For i As Integer = 0 To 9
    				.Rows.Add("AAA" & i.ToString, "BBB" & i.ToString, Now.AddDays(-i), "CCC" & i.ToString)
    			Next
    		End With
    	End Sub
    	Sub SaveCSV()
    		Using sw As New IO.StreamWriter("C:\Users\lesha\Desktop\TestCSV.csv")
    			For Each r As DataGridViewRow In DataGridView1.Rows
    				Dim coma As String = Nothing
    				For Each c As DataGridViewCell In r.Cells
    					sw.Write(coma & c.Value.ToString)
    					coma = ","
    				Next
    				sw.Write(vbCrLf)
    			Next
    		End Using
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		SaveCSV()
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, June 16, 2019 7:20 PM
  • Hello,

    The following will export a DataGridView where the first row is the column headers followed by each row of the DataGridView. If you don't want the column headers comment out the first sb.AppendLine.

    Add the following code module to your project.

    Imports System.IO
    Imports System.Runtime.CompilerServices
    Imports System.Text
    
    Public Module DataGridViewExtensions
        <Extension()>
        Public Sub ExportRows(sender As DataGridView, fileName As String, ByVal Optional delimiter As String = ",")
            If sender.RowCount > 0 Then
                Dim sb = New StringBuilder()
                Dim headers = sender.Columns.Cast(Of DataGridViewColumn)()
                sb.AppendLine(String.Join(delimiter, headers.Select(Function(column) column.HeaderText)))
    
                For Each row As DataGridViewRow In sender.Rows
    
                    If Not row.IsNewRow = True Then
                        Dim cells = row.Cells.Cast(Of DataGridViewCell)()
                        sb.AppendLine(String.Join(delimiter, cells.Select(Function(cell) cell.Value)))
                    End If
                Next
    
                File.WriteAllText(fileName, sb.ToString())
            End If
        End Sub
    End Module
    

    Simple usage, change the name of the DataGridView and change the path and file name to suit your needs.

    Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exported.csv")
    customersAccessDataGridView.ExportRows(fileName)
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by pb1990 Thursday, June 27, 2019 2:22 PM
    Sunday, June 16, 2019 8:51 PM
    Moderator
  • Hello,

    The following will export a DataGridView where the first row is the column headers followed by each row of the DataGridView. If you don't want the column headers comment out the first sb.AppendLine.

    Add the following code module to your project.

    Imports System.IO
    Imports System.Runtime.CompilerServices
    Imports System.Text
    
    Public Module DataGridViewExtensions
        <Extension()>
        Public Sub ExportRows(sender As DataGridView, fileName As String, ByVal Optional delimiter As String = ",")
            If sender.RowCount > 0 Then
                Dim sb = New StringBuilder()
                Dim headers = sender.Columns.Cast(Of DataGridViewColumn)()
                sb.AppendLine(String.Join(delimiter, headers.Select(Function(column) column.HeaderText)))
    
                For Each row As DataGridViewRow In sender.Rows
    
                    If Not row.IsNewRow = True Then
                        Dim cells = row.Cells.Cast(Of DataGridViewCell)()
                        sb.AppendLine(String.Join(delimiter, cells.Select(Function(cell) cell.Value)))
                    End If
                Next
    
                File.WriteAllText(fileName, sb.ToString())
            End If
        End Sub
    End Module

    Simple usage, change the name of the DataGridView and change the path and file name to suit your needs.

    Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exported.csv")
    customersAccessDataGridView.ExportRows(fileName)

    Hi Karen,

    does it save in "Current Directory"?

    Best Regards

    Pasquale Basilio

    Thursday, June 27, 2019 12:32 PM