Click here to Skip to main content
14,668,844 members
Rate this:
Please Sign up or sign in to vote.
Im having a problem where, if I select multiple DataGridView rows, all the information is placed into one line of the .csv file, this is giving me issues when trying to load the file back up.

Instead of having to select and saving each row individually (They are selected so they write to another file), how can I change the code below, so that for each selected row, each row is written onto another line.

What I have tried:

Private Sub btnToSession_Click(sender As Object, e As EventArgs) Handles btnToSession.Click
    'PURPOSE: Write only selected DataGridView row to Session.csv
    Dim StrExport As String = ""
    For Each C As DataGridViewColumn In dataGVGeneral.Columns
        StrExport &= "" & C.HeaderText & ","
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine
    'Only the row which is selected will be writen
    For Each R As DataGridViewRow In dataGVGeneral.Rows
        If R.Selected = True Then
            For Each C As DataGridViewCell In R.Cells
                If Not C.Value Is Nothing Then
                    StrExport &= "" & C.Value.ToString & ","
                Else
                    StrExport &= "" & "" & ","
                End If
            Next
        Else
            StrExport = StrExport.Substring(0, StrExport.Length - 1)
            StrExport &= Environment.NewLine
        End If
    Next
    'ROLE: Given ({filePath}, False)- allows for the file to be completely overwritten with new data.
    Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("E:\SAT\Work.io\Work.io\bin\Debug\Session.csv", False)
    tw.Write(StrExport)
    tw.Close()
End Sub
Posted
Updated 31-Jul-20 23:16pm

Rate this:
Please Sign up or sign in to vote.

Solution 1

Look at your code.
What do you think this does?
Else
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine
End If

Every time you go round your loop and find an non-selected row, you discard all information collected by previous iterations ... so of course you only get one row!

And thirty seconds with the debugger would have shown you that!

As I've said before: stop guessing; start thinking. It really does work out easier and quicker in the long term.

And don't concatenate strings like that: strings are immutable, so every "&" operation creates a new, bigger string. If you want to build a large string, use a StringBuilder - it's a lot more efficient!
   
Comments
Shaheer Rizwan 1-Aug-20 4:38am
   
Yeah got it thanks mate.
Shaheer Rizwan 1-Aug-20 4:40am
   
Do you know how I fix this though, (ive tried alot of things).
Say I select the 10th row, and write it to the file, it gets written to the 10th line despite nothing else in the file, how can I get it so it writes to the next available line, except of just what row number it is on the DataGridView
Rate this:
Please Sign up or sign in to vote.

Solution 2

How many more times do we need to explain this to you? You must only write the text inside the if block that tests for a selected row. You seem to be creating a much more complicated solution than is necessary. See my suggestion in the comments section at Only writing selected datagridview rows to csv.[^].

This is all you need:
' open the writer file first
Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("E:\SAT\Work.io\Work.io\bin\Debug\Session.csv", False)
    ' write the column headings if required
    For Each C As DataGridViewColumn In dataGVGeneral.Columns
        tw.Write(C.Value.ToString & ",")
    Next
    ' add a newline after the column headings
    tw.Write(Environment.NewLine)
    'Only the row which is selected will be writen
    For Each R As DataGridViewRow In dataGVGeneral.Rows
        If R.Selected = True Then
        ' if the row is selected then write all the colmns, including the emty ones
            For Each C As DataGridViewCell In R.Cells
                tw.Write(C.Value.ToString & ",")
            Next
            ' a newline cahracter at the end of the line
            tw.Write(Environment.NewLine)
        End If
    Next
    ' close the file
    tw.Close()
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100