Link to home
Start Free TrialLog in
Avatar of holemania
holemania

asked on

Streamwriter to remove blank line

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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

For intX As Integer = 0 To dgPP.Rows.Count - 2

?
You could probably just do:
'write row to file
If strRowData <> "" Then
        sr.WriteLine(strRowData)
End If

Open in new window

Avatar of holemania
holemania

ASKER

Tried both and still adding a blank line at the end of my text file.
Also tried the following without much luck.

If Not String.IsNullOrEmpty(strRowData) Then
   sr.Writeline(strRowData)
End If
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah that works.  Hmmm that's strange.  I thought it was a blank line, but guess not.
Thank you.