Export Datagrid vers CSV Problemes de headers
Bonjour a tous,
je cherche a exporter le contenu d'un datagridview (donnees et headers) vers un fichier de type csv. Sur le net, j'ai pu trouver ce morceau de code qui fonctionne tres bien pour ce qui concerne les donnees mais je ne parviens toujours pas a exporter les headers des colonnes de mon datagrid.
Pourriez vous m'aiguiller?
merci
Anthony
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
Private Sub SaveGridDataInFile(ByRef fName As String)
Dim cellvalue As String = ""
Dim rowLine As String = ""
Try
'Fill CSV with the Data from the DataGridView
Dim objWriter As New System.IO.StreamWriter(fName, True)
For j As Integer = 0 To (DataGridView1.Rows.Count - 2)
For i As Integer = 0 To (DataGridView1.Columns.Count - 1)
If Not TypeOf DataGridView1.CurrentRow.Cells.Item(i).Value Is DBNull Then
cellvalue = DataGridView1.Item(i, j).Value.ToString
Else
cellvalue = ""
End If
rowLine = rowLine & cellvalue & ","
Next i
objWriter.WriteLine(rowLine)
rowLine = ""
Next j
' Free the Object
objWriter.Dispose()
objWriter.Close()
MsgBox("Export Successfull")
Catch e As Exception
MessageBox.Show("Error occured while writing to the file." + e.ToString(), "Error")
Finally
FileClose(1)
End Try
End Sub |