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

Posted

技术标签:

【中文标题】vb.net将datagridview中的数据保存到txt并加载【英文标题】:Vb.net Save data from datagridview to txt and load it 【发布时间】:2015-04-27 08:46:13 【问题描述】:

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

Tool_1 螺丝刀 Tool_2 螺丝 Tool_3 磁铁

还有更多 我创建了项目,它有数据网格视图(两列,工具和名称)和两个按钮(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 螺丝刀”没有拆分,而是在“工具”列中 有解决办法吗?

【问题讨论】:

尝试使用逗号之类的分隔符并保存文件,这样当您尝试加载它时。会很容易分裂 【参考方案1】:

在 loadgriddata 中使用循环的这个 insetad

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

希望对你有帮助。

【讨论】:

str = thisline.split(" ") 报错“错误 1 ​​'1-dimensional array of String'类型的值不能转换为'String'。”【参考方案2】:

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

导出列表视图:

    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

【讨论】:

【参考方案3】:

下面一行是错误的。

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

【讨论】:

以上是关于vb.net将datagridview中的数据保存到txt并加载的主要内容,如果未能解决你的问题,请参考以下文章

关于VB.NET2005中dataGridView的问题

如何在 VB.NET 中将 DataGridView 导出为 Excel 格式

需要一些关于将数据放入 vb.Net 中的 DataGridView 的建议

VB.net 中的 DataGridView 不允许我更新

vb.net Datagridview添加行

如何使 datagridview 实时连接到本地存储在 VB.NET 中的访问数据库?