cells.Add(row.Cells.Item(cellNumber).Value.ToString)
Private Sub SaveGridDataInFile(ByRef fName As String) Using fSave As New SaveFileDialog fSave.FileName = fName If fSave.ShowDialog = Windows.Forms.DialogResult.OK Then fName = fSave.FileName Else Exit Sub End If End Using Try Dim lines As New List(Of String) For Each row As DataGridViewRow In DataGridView1.Rows Dim cells As New List(Of String) For cellNumber As Integer = 0 To (DataGridView1.Columns.Count - 1) If Not TypeOf row.Cells.Item(cellNumber).Value Is DBNull Then cells.Add(row.Cells.Item(cellNumber).Value.ToString) Else cells.Add("") End If Next lines.Add(String.Join(","c, cells.ToArray)) Next Dim csvString As String = String.Join(Environment.NewLine, lines.ToArray) IO.File.WriteAllText(fName, csvString) Catch e As Exception MessageBox.Show("Error occured while writing to the file." + e.ToString()) Finally End Try End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)