繁体   English   中英

从VB.net中的DataGridView将数据保存到文本文件

[英]Save data to text file from DataGridView in VB.net

我有一个包含6列的数据网格视图...

我需要将所有行和列保存在每个字符串一行中

例如:

表

当将此表的数据保存到文本文件中时,它应如下所示:

John
0000000000
M
Mike
0000000000
M

编辑:

我已经尝试过这种事情:

Dim name As String = DataGridView1.Rows(e.RowIndex).Cells(1).Value
Dim mobile As String = DataGridView1.Rows(e.RowIndex).Cells(2).Value
Dim gender As String = DataGridView1.Rows(e.RowIndex).Cells(3).Value

Dim fileName As String = CurDir() + "\Measures.txt"
Dim fileNum As Integer = FreeFile()

FileOpen(fileNum, fileName, OpenMode.Output)

PrintLine(fileNum, name)
PrintLine(fileNum, mobile)
PrintLine(fileNum, gender)

注意:数据应另存为字符串

任何帮助,将不胜感激,谢谢

这种方法应该使您接近。 您仍然需要进行验证等

Private Function exportDataGridView(filePath As String, dgv As DataGridView) As Boolean

    Using sw As New StreamWriter(filePath)

        For Each dr As DataGridViewRow In dgv.Rows
            For Each dc As DataGridViewCell In dr.Cells
                sw.WriteLine(dc.Value.ToString)
            Next
        Next
    End Using

    Return True

End Function

此代码将使用循环在每个单元格中运行。 它还将忽略最后一行,即DataGirdView末尾的空行。这是代码:

Using writer As New System.IO.StreamWriter(filePath) For row As Integer = 0 To DataGridView1.RowCount - 2 For col As Integer = 0 To DataGridView1.ColumnCount - 1 writer.WriteLine(DataGridView1.Rows(row).Cells(col).Value) Next Next End Using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM