Results 1 to 7 of 7

Thread: Export Datagrid to text file

  1. #1
    knockyo is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2006
    Posts
    76
    Reputation
    85

    Export Datagrid to text file

    how to export below picture datagrid to the text file format? (Windows Form)



    Hope has some code.

    Thanks.

  2. #2
    HeavenCore's Avatar
    HeavenCore is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Apr 2007
    Location
    Bolton, England
    Posts
    77
    Reputation
    78
    text file format? you mean dump the data as binary or CSV? or the actual code to draw the grid?

  3. #3
    knockyo is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Sep 2006
    Posts
    76
    Reputation
    85
    Quote Originally Posted by HeavenCore View Post
    text file format? you mean dump the data as binary or CSV? or the actual code to draw the grid?


    actually i want export to notepad/ text file format. No matter is CSV or what, as long as can open by notepad

    thanks your advice

  4. #4
    ss7thirty is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2005
    Location
    New Jersey, US
    Posts
    455
    Reputation
    174
    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.

    Code:
            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

  5. #5
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,027
    Reputation
    1705
    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

  6. #6
    eject is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2007
    Posts
    1
    Reputation
    0
    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

  7. #7
    Piplfox is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Posts
    1
    Reputation
    0
    What dgCarton is stand for. Is Cartun Table name.
    Quote Originally Posted by eject View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking