Hello,
I tried to export data from DataGridView as txt file using vb.net, but it is not aligning properly.
Below is the code sample that I used.
Dim filePath As String = "c:\Result.txt"
DataTableToTextFile2(dt, filePath)
Using myFileDialog As New SaveFileDialog()
myFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
myFileDialog.FilterIndex = 1
myFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
myFileDialog.CheckFileExists = False
Dim result As DialogResult = myFileDialog.ShowDialog()
If result = DialogResult.OK Then
Dim numCols As Integer = GridView1.ColumnCount
Dim numRows As Integer = GridView1.RowCount - 1
Dim strDestinationFile As String = myFileDialog.FileName
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
'writing the header
For count As Integer = 0 To numCols - 1
tw.Write(GridView1.Columns(count).HeaderText)
If (count <> numCols - 1) Then
tw.Write(" ")
End If
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 1
For count2 As Integer = 0 To numCols - 1
tw.Write(GridView1.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then
tw.Write(" ")
End If
Next
tw.WriteLine()
Next
tw.Close()
MsgBox("Export complete", MsgBoxStyle.Information)
'frmAnnotationG.txtLanguage.Text = System.IO.File.ReadAllText(strDestinationFile)
End If
End Using