แค่ Loop แล้วเขียนลง Text File น่ะครับ ลองดูตัวอย่างครับ
Code (VB.NET)
Private Sub button1_Click(sender As Object, e As EventArgs)
Dim strValue As String = String.Empty
For i As Integer = 0 To dataGridView1.Rows.Count - 2
For j As Integer = 0 To dataGridView1.Rows(i).Cells.Count - 1
If Not String.IsNullOrEmpty(dataGridView1(j, i).Value.ToString()) Then
If j > 0 Then
strValue = strValue & "," & dataGridView1(j, i).Value.ToString()
Else
If String.IsNullOrEmpty(strValue) Then
strValue = dataGridView1(j, i).Value.ToString()
Else
strValue = strValue + Environment.NewLine & dataGridView1(j, i).Value.ToString()
End If
End If
End If
' strValue = strValue + Environment.NewLine;
Next
Next
Dim strFile As String = "C:\YourCSVFile.csv"
If File.Exists(strFile) AndAlso Not String.IsNullOrEmpty(strValue) Then
File.WriteAllText(strFile, strValue)
End If
End Sub