[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Streamwriter to remove blank line

Posted on 2014-03-18
7
Medium Priority
?
1,135 Views
Last Modified: 2014-03-18
Trying to write from a datagridview to a text file.  I need to remove the blank line at the end of the text file.  Anything I can do with the following code?

Dim sr As StreamWriter
    Dim strDelimiterType As String
    Sub Export()

        strFileName = "test.txt"

        strDelimiterType = ","

        'create streamwriter to open and write contents from datagrid
      
        sr = File.CreateText(txtFilePath.Text & "\" & strFileName)


        'creat variable to hold delimiter
        Dim strDelimiter As String = strDelimiterType

        'create variable to hold total number of columns
        Dim intColumnCount As Integer = dgPP.Columns.Count - 1

        'create a variable to hold the row data
        Dim strRowData As String = ""

        'get rest of data to file
        For intX As Integer = 0 To dgPP.Rows.Count - 1
            'reset strRowData
            strRowData = ""

            For intRowData As Integer = 0 To intColumnCount
                strRowData += Replace(dgPP.Rows(intX).Cells(intRowData).Value, strDelimiter, "") & _
                IIf(intRowData < intColumnCount, strDelimiter, "")
            Next intRowData

            'write row to file
            sr.WriteLine(strRowData)
        Next intX

        'close streamwriter
        sr.Close()
    End Sub

Open in new window

0
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39937116
For intX As Integer = 0 To dgPP.Rows.Count - 2

?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39937234
You could probably just do:
'write row to file
If strRowData <> "" Then
        sr.WriteLine(strRowData)
End If

Open in new window

0
 

Author Comment

by:holemania
ID: 39937504
Tried both and still adding a blank line at the end of my text file.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:holemania
ID: 39937509
Also tried the following without much luck.

If Not String.IsNullOrEmpty(strRowData) Then
   sr.Writeline(strRowData)
End If
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39937597
then I think you are wanting to skip the WriteLine for the last line, and make that a Write instead?
maybe you are misleaded that the file has an "empty" line at the end...
Dim sr As StreamWriter
    Dim strDelimiterType As String
    Sub Export()

        strFileName = "test.txt"

        strDelimiterType = ","

        'create streamwriter to open and write contents from datagrid
      
        sr = File.CreateText(txtFilePath.Text & "\" & strFileName)


        'creat variable to hold delimiter
        Dim strDelimiter As String = strDelimiterType

        'create variable to hold total number of columns
        Dim intColumnCount As Integer = dgPP.Columns.Count - 1

        'create a variable to hold the row data
        Dim strRowData As String = ""

        'get rest of data to file
        For intX As Integer = 0 To dgPP.Rows.Count - 1
            'reset strRowData
            strRowData = ""

            For intRowData As Integer = 0 To intColumnCount
                strRowData += Replace(dgPP.Rows(intX).Cells(intRowData).Value, strDelimiter, "") & _
                IIf(intRowData < intColumnCount, strDelimiter, "")
            Next intRowData

            'write row to file
            if intX = dgPP.Rows.Count - 1 then
            sr.Write(strRowData)
            else 
            sr.WriteLine(strRowData)
           end if

        Next intX

        'close streamwriter
        sr.Close()
    End Sub
                                  

Open in new window

0
 

Author Comment

by:holemania
ID: 39937664
Ah that works.  Hmmm that's strange.  I thought it was a blank line, but guess not.
0
 

Author Closing Comment

by:holemania
ID: 39937667
Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question