this is my first post im not good in english ; and here is my code and i hope it's will work fine 4u
Private Sub ExporterDtgView()
Try
'chose the distination file
If SaveFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
'It validates the edition of the DataGridView
DataGridView1.EndEdit()
'We are preparing a brief to write formatted data to the file
Dim ToSave As New StringBuilder()
'We'll put the headers as required
Dim Headers As String = String.Empty
For index As Integer = 0 To DataGridView1.Columns.Count - 1
Headers &= DataGridView1.Columns(index).HeaderText & ";"
Next
'The loop adds a ";" at the end if it's useless
Headers = Headers.Remove(Headers.LastIndexOf(";"), 1)
'Now it is stored in the memory
ToSave.AppendLine(Headers)
'Loop over all available lines
For i As UInt64 = 0 To DataGridView1.Rows.Count - 1
'It is a variable to store a line
Dim OneRow As String = String.Empty
'You can do a loop on all the columns available if the line is not empty
If DataGridView1.Rows(i).IsNewRow = False Then
For j As Integer = 0 To DataGridView1.Rows(i).Cells.Count - 1
OneRow &= DataGridView1.Rows(i).Cells(j).Value & ";"
Next
OneRow = OneRow.Remove(OneRow.LastIndexOf(";"), 1)
ToSave.AppendLine(OneRow)
End If
Next
'now trying to write the file
IO.File.WriteAllText(SaveFileDialog1.FileName, ToSave.ToString(), Encoding.Default)
End If
Catch ex As Exception
MessageBox.Show(String.Format("{0}{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace))
End Try
End Sub