Export Datagrid to text file

knockyo

Well-known member
Joined
Sep 20, 2006
Messages
78
Programming Experience
1-3
how to export below picture datagrid to the text file format? (Windows Form)

gridnk6.jpg


Hope has some code.

Thanks.
 
Here is some 2005 code that i put together in about thirty seconds for outputting a datagridviews contents, not even sure if it works but the logic will be the same pretty much.

VB.NET:
        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
 
DataGrid should take its data froma dataset. DataSet can be written to file as XML using its WriteXml method. i.e. One line of code will solve your problem
 
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.

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
 
Back
Top