首页 > 解决方案 > 如何使用 CSV Datasource-VB.net 以另一种形式将 DataGridView 行移动到另一个 DataGridView

问题描述

我使用 CSV 从表单中的 DataGridView 加载数据和保存数据。我现在想要它,因此通过按钮单击事件,我可以将通过 DataGridView 上的复选框列选中的行发送到另一个以另一种形式托管的 dataGridView。

如果有帮助,这是我用来加载和保存 DataGridView 内容的负载。

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 

标签: vb.netcsvdatagridview

解决方案


推荐阅读