text file format? you mean dump the data as binary or CSV? or the actual code to draw the grid?
Dim dgvc As DataGridViewCell
Dim sw As New System.IO.StreamWriter("c:\test.txt")
For Each dgvr As DataGridViewRow In DataGridView1.Rows
Dim intCellCount As Integer = dgvr.Cells.Count
Dim intCounter As Integer = 1
For Each dgvc In dgvr.Cells()
If intCounter <> intCellCount Then
sw.Write(dgvc.Value.ToString & "|")
Else
sw.WriteLine(dgvc.Value.ToString)
End If
intCounter += 1
Next
Next
Private Sub BtnGentxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGentxt.Click
Dim a, b As Integer
Dim Output As String
Dim numRows As Integer = dgCarton.BindingContext(dgCarton.DataSource, dgCarton.DataMember).Count
Const Seperator As String = ","
Dim FileName As String = "C:\carton.txt"
FileOpen(1, FileName, OpenMode.Output)
For a = 0 To numRows - 1
For b = 0 To 5 '5 is number of columns
If b = 5 Then
'Do not add the seperator at the end of the line.
Output = Output & CStr(dgCarton.Item(a, b))
Else
Output = Output & CStr(dgCarton.Item(a, b)) & Seperator
End If
Next
b = 0
Print(1, Output & vbNewLine)
Output = ""
Next
FileClose(1)
End Sub
Hope this want can help you.
This code will export a datagrid into a text file line by line and separate ur columns by comma.