对于下面的代码,我想在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
还有导出文件的图片:
Actual TXT export Actual Code for input my mandatory text before DGV export
谢谢!
答案 0 :(得分:0)
提供一些调试细节,例如Rows.Count
等。
如果它没有进入循环内部甚至有行,那么最好尝试访问DataSource中的对象。
类似ForEach的东西在((DataTable)DataGridView1.DataSource).Rows