Results 1 to 4 of 4

Thread: [RESOLVED] Write a .txt file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,406

    Resolved [RESOLVED] Write a .txt file

    Hi to all:

    I need here a help to write a .txt file with a specific format.
    This piece of code write the a .txt file:

    Code:
    If File.Exists(fileLoc) Then
                Using sw As StreamWriter = New StreamWriter(fileLoc)
                    sw.WriteLine("secao;classe;descricao;tipogaiola;fichajulgamento;anolimiteanilha;numeroaves;campob;campoc")
                    For i As Integer = 0 To DataGridView1.Rows.Count - 1
                        Dim columnIndex As Integer = 0
                        Do Until columnIndex = columnsCount
                            sw.WriteLine(DataGridView1.Item(columnIndex, i).Value.ToString")
                            columnIndex += 1
                        Loop
                    Next
                End Using
            End If
    in this way:

    secao;classe;descricao;tipogaiola;fichajulgamento;anolimiteanilha;numeroaves;campob;campoc
    A
    1
    Equipa 4 Harz
    canto
    and i would like to sort this way:

    secao;classe;descricao;tipogaiola;fichajulgamento;anolimiteanilha;numeroaves;campob;campoc
    A;1;Equipa 4 Harz;canto
    Any help please?
    Thanks

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Write a .txt file

    You used WriteLine, so it is dutifully doing what you told it to do... it's writing everything to a new line... what you probably want is .Write - you'll also want to add a semicolon to the text to... and then only use WriteLine when you want to go to the next line.

    I'vew done similar things... but my data is in an array, so I was able to use the array.join function to combine it into a single string, and then I add it to a list (Of String) ... then use WriteAllLines(myList.ToArray) when I'm ready to write the file. May or may not be an option here... it looks like you're saving grid contents ... but it might be worth looking at. Or not. Just throwing it out there as an alternative.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,406

    Re: Write a .txt file

    hi had try this and resolve:

    Code:
    If File.Exists(fileLoc) Then
                Using sw As StreamWriter = New StreamWriter(fileLoc)
                    sw.WriteLine("secao;classe;descricao;tipogaiola;fichajulgamento;anolimiteanilha;numeroaves;campob;campoc")
                    For i As Integer = 0 To DataGridView1.Rows.Count - 1
                        Dim columnIndex As Integer = 0
                        Do Until columnIndex = columnsCount
                            sw.Write(DataGridView1.Item(columnIndex, i).Value.ToString)
                            sw.Write(";")
                            columnIndex += 1
                        Loop
                        sw.WriteLine("")
                    Next
                End Using
            End If
    thanks

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: [RESOLVED] Write a .txt file

    Just as a side comment, I'd replace this:
    Code:
                            sw.Write(DataGridView1.Item(columnIndex, i).Value.ToString)
                            sw.Write(";")
    with this:
    Code:
                            sw.Write(string.format("{0};", DataGridView1.Item(columnIndex, i).Value))
    Probably more of a personal choice than any real performance... also one thing, with this code (both yours and mine) you'll get a trailing; on the end of everyline. which you may or may not want. In that case, it's easier to modify your code to check to see if it's on the last col before writing out the ;


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width