Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using a CSV to load data and save data from the DataGridView I have in a form. I would now like it so with a button click event I can send a row checked via the checkbox column on the DataGridView, to another dataGridView which is hosted in another form.

If it helps, here is the load im using to load and save the DataGridView contents.

What I have tried:

Private Sub btnLoadDGVGeneral_Click(sender As Object, e As EventArgs) Handles btnLoadDGVGeneral.Click
    ' PURPOSE: Load CSV file containing tasks into DataGridView
    ' Clearing DGV Rows allows for Tasks to not double up when rebooting the program
    dataGVGeneral.Rows.Clear()
    'New Variable: fname, represents File Path of CSV as String
    Dim fname As String = "E:\SAT\Work.io\Work.io\bin\Debug\ListofTasks.csv"
    Dim reader As New StreamReader(fname, Encoding.Default)
    Dim sline As String = ""
    Dim colsexpected As Integer = 7
    Dim r As Integer = 0
    'StreamReader will the file Line by Line, and add it to the variable sline
    'First sline statement is Out of Loop, as first line of CSV contains headings to what each figure represents in a line.
    sline = reader.ReadLine
    Do
        'Now sline will read the 2nd line and so forth
        sline = reader.ReadLine
        'If no value is found on that line of the CSV file (at the end), then the loop will exit
        If sline Is Nothing Then Exit Do
        'Details is as a variable, which when called upon, places each value into different columns for that row
        Dim details() As String = sline.Split(",")
        dataGVGeneral.Rows.Add()
        For i As Integer = 0 To 6
            dataGVGeneral.Rows(r).Cells(i).Value = details(i)
        Next
        'Increments value of "r" by 1, meaning next line of CSV will be added to the next row.
        Dim v As Integer = r + 1
        r = v
    Loop
    reader.Close()
End Sub



Private Sub btnSaveGeneralDGV_Click(sender As Object, e As EventArgs) Handles btnSaveGeneralDGV.Click
    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()
End Sub 
Posted
Updated 28-Jul-20 21:50pm

1 solution

You should create a method in the second form that accepts a DataGridViewRow, and adds it to its own DataGridView. Then you just need to call that method with the aprropriate data.
 
Share this answer
 
Comments
Shaheer Rizwan 29-Jul-20 19:36pm    
can that be via the checkbox column. say if i checked it, and pressed a button, that data will be copied over to another DataGridView in another form. Is there some sample code I can find somewhere, I am really stuck
Richard MacCutchan 30-Jul-20 4:43am    
Nothing will happen unless you write the code that makes it happen.

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