Dim numCols As Integer = dgv_anc.ColumnCount
Dim numRows As Integer = dgv_anc.RowCount - 1
Dim strDestinationFile As String = "D:\\output.txt"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
'writing the header
For count As Integer = 1 To numCols - 1
tw.Write(dgv_anc.Columns(count).HeaderText)
If (count <> numCols - 1) Then
tw.Write("| ")
End If
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 0
For count2 As Integer = 1 To numCols - 1
tw.Write(dgv_anc.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then
tw.Write("| ")
End If
Next
tw.WriteLine()
Next
tw.Close()
MsgBox("data exported to the textfile succsessfully")
Dim exportRecord = txtExport.Text.Split(",")
For count As Integer = 0 To exportRecord.Length - 1
For count2 As Integer = 1 To numCols - 1
tw.Write(dgv_anc.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then
tw.Write("| ")
End If
Next
tw.WriteLine()
Next
tw.Close()