vb Code:
Imports Microsoft.VisualBasic.FileIO
Module Module1
<System.Diagnostics.DebuggerStepThrough()> _
<System.Runtime.CompilerServices.Extension()> _
Public Function LoadTextFile( _
ByVal sender As DataGridView, _
ByVal FileName As String, _
ByRef Errors As List(Of String)) As Boolean
If Not IO.File.Exists(FileName) Then
Errors.Add("Failed to locate '" & FileName & "'")
Return False
End If
If sender.ColumnCount <> 16 Then
Errors.Add("Must have three columns to load your text file., there are " & _
sender.ColumnCount.ToString & " columns presently.")
Return False
End If
Using MyReader As New TextFieldParser(FileName)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim Line As String()
sender.SuspendLayout()
Try
While Not MyReader.EndOfData
Try
Line = MyReader.ReadFields()
'
' For every column per line
' there must be a column in the DataGridView
'
sender.Rows.Add(New Object() {Line(0), Line(1), Line(2), Line(3), Line(4), Line(5), Line(6), Line(7), Line(8), Line(9), Line(10), cbool(Line(11)), Line(12), Line(13), Line(14), Line(15)})
sender.endedit
' Line(4), Line(5), Line(6), Line(7), Line(8), Line(9), Line(10), Line(11), Line(12), Line(13), Line(14), Line(15), Line(16)})
Catch ex As FileIO.MalformedLineException
'
' Report problem
'
Errors.Add(ex.Message)
Catch ex As Exception
Errors.Add(ex.Message)
End Try
End While
Finally
sender.ResumeLayout()
End Try
End Using
Return Errors.Count = 0
End Function
End Module