Vb.net 将datagridview中的数据保存到txt并加载

Vb.net Save data from datagridview to txt and load it

我是新程序员,正在学习 Visual Basic,并完成我的考试 我拥有的数据

Tool_1 screwdriver
Tool_2 screw
Tool_3 Magnet

还有更多
我创建了项目,它有数据网格视图(两列,工具和名称)和两个按钮(btSave & btOpen)
我只是用这段代码试试

Private Sub btSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSave.Click
SaveGridData(DataGridView1, ThisFilename)
End Sub
    Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
        ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
        ThisGrid.SelectAll()
        IO.File.WriteAllText(Filename, ThisGrid.GetClipboardContent().GetText.TrimEnd)
        ThisGrid.ClearSelection()
    End Sub
    Private Sub btOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btOpen.Click
    LoadGridData(DataGridView1, ThisFilename)
End Sub

Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
        ThisGrid.Rows.Clear()
        For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)
            ThisGrid.Rows.Add(Split(THisLine, " "))
        Next
    End Sub

当我保存文件时,txt 文件没问题,但是当我想加载文本时 "Tool_1 Screwdriver" 没有拆分,而是在 "Tools" 列
有解决办法吗?

在加载网格数据时使用这个代替循环

For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)
dim str as string()
str=thisline.split(" ")
ThisGrid.Rows.Add(str(0),str(1))
Next

希望对您有所帮助。

嘿,我也为此苦苦挣扎,但我有一些有用的代码:

导出列表视图:

    System.IO.Directory.CreateDirectory("C:\RS Account Maker\Accounts" & "\")
    SaveFileDialog1.ShowDialog()
    Dim Path As String = SaveFileDialog1.FileName
    Dim AllItems As String = ""

    Try

        For Each item As ListViewItem In ListView1.Items
            AllItems = AllItems & item.Text & "#" & item.SubItems(1).Text & "#" & item.SubItems(2).Text & vbNewLine
        Next

        AllItems = AllItems.Trim

    Catch ex As Exception
    End Try


    Try
        If My.Computer.FileSystem.FileExists(Path) Then
            My.Computer.FileSystem.DeleteFile(Path)
        End If

        My.Computer.FileSystem.WriteAllText(Path, AllItems, False)

    Catch ex As Exception
        MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error ")
    End Try

导入列表视图:

    OpenFileDialog1.ShowDialog()
    Dim Path As String = OpenFileDialog1.FileName
    Dim AllItems As String



    Try
        AllItems = My.Computer.FileSystem.ReadAllText(Path)

        Dim ItemLines As New TextBox
        ItemLines.Text = AllItems


        For Each line As String In ItemLines.Lines

            Dim a1() As String = line.Split("#")

            Dim ItemName As String = a1(0)
            Dim SubItem1 As String = a1(1)
            Dim SubItem2 As String = a1(2)

            Dim Item As New ListViewItem(ItemName)

            Item.SubItems.Add(SubItem1)
            Item.SubItems.Add(SubItem2)

            ListView1.Items.AddRange(New ListViewItem() {Item})

        Next

    Catch ex As Exception
        MsgBox("Error" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Error ")
    End Try

下面一行是错误的。

ThisGrid.Rows.Add(Split(THisLine, " "))

以上代码修改如下

Dim ThisFilename As String = Application.StartupPath & "\MyData.dat"

    Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        SaveGridData(Datagrid1, ThisFilename)

    End Sub

    Private Sub butLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        LoadGridData(Datagrid1, ThisFilename)

    End Sub

    Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)

        ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText

        ThisGrid.SelectAll()

        IO.File.WriteAllText(Filename, ThisGrid.GetClipboardContent().GetText.TrimEnd)

        ThisGrid.ClearSelection()

    End Sub

    Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)

        ThisGrid.Rows.Clear()

        For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)

            ThisGrid.Rows.Add(Split(THisLine, ControlChars.Tab))

        Next

    End Sub