-1

对于下面的代码,我想在 TXT 中导出的每一行的开头放置以下文本:“S,1,______,_,__;” + 从数据库中导出的行。

如何为导出文本的每一行添加?目前我只有第一行的文字(检查图片附件)

  If DataGridView1.RowCount = 0 Then
        MessageBox.Show("Lista este goala")
    Else
        If Directory.Exists("C:\test") = False Then
            Directory.CreateDirectory("C:\test")
        End If
        Dim sFile As String = "C:\test\test.txt"
        If File.Exists(sFile) = True Then
            My.Computer.FileSystem.DeleteFile(sFile,  

            FileIO.UIOption.OnlyErrorDialogs,
            FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
        End If

        Using f As New IO.StreamWriter(sFile, True)




            Dim col As String = ""
            Dim a As String = "S,1,______,_,__;"
            Dim row As String = ""
            Dim i As Integer = 0

            For Each r As DataGridViewRow In DataGridView1.Rows
                For Each c As DataGridViewColumn In DataGridView1.Columns
                    row = row & Convert.ToString(r.Cells(c.HeaderText).Value) & ";"
                Next
                If i < DataGridView1.Rows.Count - 1 Then row &= Environment.NewLine



            Next
            f.WriteLine(a & row)


        End Using


        Using f2 As New IO.StreamWriter(sFile, True)

            Dim col As String = ""

            Dim row As String = ""
            Dim i As Integer = 0

            For Each r As DataGridViewRow In DataGridView3.Rows
                For Each c As DataGridViewColumn In DataGridView3.Columns
                    row = row & Convert.ToString(r.Cells(c.HeaderText).Value) & ";"
                Next
                If i < DataGridView3.Rows.Count - 1 Then Row &= Environment.NewLine
            Next


            f2.WriteLine("T,1,______,_,__;" & row)
            f2.Close()



            MessageBox.Show("Bon printat")
        End Using
    End If

还有一张带有导出文件的图片:

  • 红圈线是我需要导出的方式
  • 代码中的红色圆圈字符串是我的输入,对于每一行都是强制性的,然后是 DGV 行导出。

实际 TXT 导出 用于在 DGV 导出之前输入我的强制文本的实际代码

谢谢!

4

1 回答 1

0

提供一些调试细节,比如什么是Rows.Count等。

如果它没有进入循环甚至有行,最好尝试访问 DataSource 中的对象。类似 ForEach 循环的东西((DataTable)DataGridView1.DataSource).Rows

于 2018-03-19T09:26:41.793 回答