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
You could probably just do:
'write row to file
If strRowData <> "" Then
sr.WriteLine(strRowData)
End If
ASKER
Tried both and still adding a blank line at the end of my text file.
ASKER
Also tried the following without much luck.
If Not String.IsNullOrEmpty(strRo wData) Then
sr.Writeline(strRowData)
End If
If Not String.IsNullOrEmpty(strRo
sr.Writeline(strRowData)
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah that works. Hmmm that's strange. I thought it was a blank line, but guess not.
ASKER
Thank you.
?