Click here to Skip to main content
15,501,036 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below is used to write all data/enteries in the DataGridView to the csv. file, I would like to know how I can modify this, so that only rows selected via the dataGridViewCheckboxColumn will be written to the file, and not the whole thing.

What I have tried:

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

For Each R As DataGridViewRow In dataGVGeneral.Rows
    For Each C As DataGridViewCell In R.Cells
        If Not C.Value Is Nothing Then
            StrExport &= """" & C.Value.ToString & ""","
        Else
            StrExport &= """" & "" & ""","
        End If
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine
Next

Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("E:\SAT\Work.io\Work.io\bin\Debug\ListofTasks.csv", False)
tw.Write(StrExport)
tw.Close()
Posted
Updated 29-Jul-20 22:37pm
Comments
Garth J Lancaster 30-Jul-20 3:28am    
c'mon - you've been through iterations of this before - if you have a checkbox column in the datagridview, then, while you're looping through the datagridview rows, if the cherckbox column .Selected = True then write that row, but build that row from the other columns not including the checkbox column
Shaheer Rizwan 30-Jul-20 3:32am     CRLF
sorry, im a bit of a slow learner and noob. well thanks for this, will the If clmSelected.Selected = True Then statement, before StrExport = StrExport.Substring(0, StrExport.Length -1)?
Richard MacCutchan 30-Jul-20 5:10am    
Yes, just test the checkbox value of each entry as you process them. Why is that so complicated?
Shaheer Rizwan 30-Jul-20 6:02am    
its just not working and im confused man
Richard MacCutchan 30-Jul-20 6:09am     CRLF
The logic is fairly simple:
For Each Row in your datagridview:    
    If the checkbox column value is True
        Write the other column cells to the csv file
    End If
End For
So all you need to do is fill in the details ...

1 solution

Come on, this isn't complicated:
1) Identify the column containing the checkbox.
2) Inside your Rows loop, cast the current row / checkbox column cell to a check box, and test it's "Checked" property. If it's true, add it to the CSV. If it isn't, skip the row.

What part of that is difficult for you?
 
Share this answer
 
Comments
Shaheer Rizwan 30-Jul-20 3:40am     CRLF
Im just newbie man. I get confused by what does what even though ive read it a million times. Anyways, will this work For Each R As DataGridViewRow In dataGVGeneral.Rows If clmSelected.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 StrExport = StrExport.Substring(0, StrExport.Length - 1) StrExport &= Environment.NewLine End If Next
Garth J Lancaster 30-Jul-20 3:46am    
will this work ? you ask - test it, find out, experiment - Information Technology IS a science - formulate a hypothesis (write some code), test it, modify it and try again
Shaheer Rizwan 30-Jul-20 4:01am     CRLF
well i tried this and just no luck, I put the it before the For Loops, no luck. 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 If clmSelected.Selected = True Then For Each R As DataGridViewRow In dataGVGeneral.Rows For Each C As DataGridViewCell In R.Cells If Not C.Value Is Nothing Then StrExport &= """" & C.Value.ToString & """," Else StrExport &= """" & "" & """," End If Next StrExport = StrExport.Substring(0, StrExport.Length - 1) StrExport &= Environment.NewLine Next Dim tw As System.IO.TextWriter = New System.IO.StreamWriter("E:\SAT\Work.io\Work.io\bin\Debug\ListofTasks.csv", False) tw.Write(StrExport) tw.Close() EndIf
OriginalGriff 30-Jul-20 4:55am     CRLF
No. Why should it work? That's just "coding by guesswork" - and that never works well. Go back to your original code, read what I said again, and think about what you need to do.
Shaheer Rizwan 30-Jul-20 6:07am    
can you give me a clue or something man im really stuck :(

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900