Hello Paul,
Here is a sample procedure that will output the contents of a DataGridView to a CSV file.
'* Add the following to your Imports section of your form:
'* Imports System.io
Private Sub subExportDGVToCSV(ByVal strExportFileName As String, _
ByVal DataGridView As DataGridView, _
Optional ByVal blnWriteColumnHeaderNames As Boolean = False, _
Optional ByVal strDelimiterType As String = ",")
'* Parameters Description:
'* --------------------------------------------------------------
'* strExportFileName = The name of the file to export to.
'* DataGridView = The name of the DataGridView on your form.
'* blnWriteColumnHeaderNames = YES/NO for writing the column
'* names as the first line of the CSV file. This will cause
'* programs like Excel to argue but still open the file.
'* strDelimiterType = The type of delimiter you want to use.
'* Examples: TAB (vbTab) or Comma (",")
'* --------------------------------------------------------------
'* Create a StreamWriter object to open and write contents
'* of the DataGridView columns and rows.
Dim sr As StreamWriter = File.CreateText(strExportFileName)
'* Create a variable to hold the delimiter type
'* (i.e., TAB or comma or whatever you choose)
'* The default for this procedure is a comma (",").
Dim strDelimiter As String = strDelimiterType
'* Create a variable that holds the total number of columns
'* in the DataGridView.
Dim intColumnCount As Integer = DataGridView.Columns.Count - 1
'* Create a variable to hold the row data
Dim strRowData As String = ""
'* If the CSV file will have column names then write that data
'* as the first line of the file.
If blnWriteColumnHeaderNames Then
'* Interate through each column and get/write the column name.
For intX As Integer = 0 To intColumnCount
'* The IIf statement will not put a delimiter after the
'* last value added.
'* The Replace function will remove the delimiter
'* from the field data if found.
strRowData += Replace(DataGridView.Columns(intX).Name, strDel imiter, "") & _
IIf(intX < intColumnCount, strDelimiter, "")
Next intX
'* Write the column header data to the CSV file.
sr.WriteLine(strRowData)
End If '* If blnWriteColumnHeaderNames
'* Now collect data for each row and write to the CSV file.
'* Loop through each row in the DataGridView.
For intX As Integer = 0 To DataGridView.Rows.Count - 1
'* Reset the value of the strRowData variable
strRowData = ""
For intRowData As Integer = 0 To intColumnCount
'* The IIf statement will not put a delimiter after the
'* last value added.
'* The Replace function will remove the delimiter
'* from the field data if found.
strRowData += Replace(DataGridView.Rows(intX).Cells(intRowData). Value, strDelimiter, "") & _
IIf(intRowData < intColumnCount, strDelimiter, "")
Next intRowData
'* Write the row data to the CSV file.
sr.WriteLine(strRowData)
Next intX
'* Close the StreamWriter object.
sr.Close()
'* You are done!
MsgBox("Done!")
End Sub
Here is an example of calling the above procedure:
Call subExportDGVToCSV("C:\Test.csv", Me.dg, False, ",")
Best Regards,
Earl Francis
|