vb Code:
  1. Imports Microsoft.VisualBasic.FileIO
  2. Module Module1
  3.     <System.Diagnostics.DebuggerStepThrough()> _
  4.     <System.Runtime.CompilerServices.Extension()> _
  5.     Public Function LoadTextFile( _
  6.       ByVal sender As DataGridView, _
  7.       ByVal FileName As String, _
  8.       ByRef Errors As List(Of String)) As Boolean
  9.  
  10.         If Not IO.File.Exists(FileName) Then
  11.             Errors.Add("Failed to locate '" & FileName & "'")
  12.             Return False
  13.         End If
  14.  
  15.         If sender.ColumnCount <> 16 Then
  16.  
  17.             Errors.Add("Must have three columns to load your text file., there are " & _
  18.                        sender.ColumnCount.ToString & " columns presently.")
  19.  
  20.             Return False
  21.  
  22.         End If
  23.  
  24.         Using MyReader As New TextFieldParser(FileName)
  25.             MyReader.TextFieldType = FileIO.FieldType.Delimited
  26.             MyReader.Delimiters = New String() {","}
  27.             Dim Line As String()
  28.  
  29.             sender.SuspendLayout()
  30.  
  31.             Try
  32.                 While Not MyReader.EndOfData
  33.                     Try
  34.                         Line = MyReader.ReadFields()
  35.                         '
  36.                         ' For every column per line
  37.                         ' there must be a column in the DataGridView
  38.                         '
  39.                         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)})
  40.                         sender.endedit
  41.                         ' 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)})
  42.                     Catch ex As FileIO.MalformedLineException
  43.                         '
  44.                         ' Report problem
  45.                         '
  46.                         Errors.Add(ex.Message)
  47.                     Catch ex As Exception
  48.                         Errors.Add(ex.Message)
  49.                     End Try
  50.                 End While
  51.             Finally
  52.                 sender.ResumeLayout()
  53.             End Try
  54.         End Using
  55.  
  56.         Return Errors.Count = 0
  57.  
  58.     End Function
  59.    
  60. End Module