Need to read specific records in a CSV file and place them into a DataGridView

  • Thread starter Thread starter Cameron Evenson
  • Start date Start date
C

Cameron Evenson

Guest
I have a CSV file and the records are organized so that the Column headings are set into the first column of data. I need to select only specific records in that the first column with its corresponding data. I have included an image of the records I need form the CSV file in the image, the file generally is 40 rows long with 3 records per row.

|1302691.jpg


I have looked into the process of using a TextFileReader to pull in the CSV file, but I was successful in bringing in the whole file but have not been as successful in pulling out individual pieces from the file and just displaying them as a Grid. Sadly, I am under a time crunch and have sought your wise counsel.

The code I currently have been working on takes into account two CSV file, as reading the first CSV will provide Column headings for the second. The Code I currently have is as follows. The Subroutine is called from and builds two DataTables which in turn populate the DataGridView on the Form.


Private Shared Sub GetCSVData(sPath As String, filetype As String)
' Need to build a DataTable from the CSV files but need to take into consideration the header
' and data files

Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(sPath)

TextFileReader.TextFieldType = FileIO.FieldType.Delimited
TextFileReader.SetDelimiters(",")

Dim Column As DataColumn
Dim Row As DataRow
Dim UpperBound As Int32
Dim ColumnCount As Int32
Dim CurrentRow As String()

While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
If Not CurrentRow Is Nothing Then
''# Check if DataTable has been created
Select Case filetype
Case "header"
If HeaderTable Is Nothing Then
HeaderTable = New DataTable("HeaderTable")
HeaderTable.Columns.AddRange(New DataColumn() {New DataColumn("Sample Name", GetType(String)),
New DataColumn("Results Created", GetType(String)),
New DataColumn("Injection Date", GetType(String)),
New DataColumn("Location", GetType(String)),
New DataColumn("Inj", GetType(Integer))})

End If
Row = HeaderTable.NewRow

Row("Sample Name") = CurrentRow("Sample Name").ToString
Row("Results Created") = CurrentRow("Results Created").ToString
Row("Injection Date") = CurrentRow("Injection Date").ToString
Row("Location") = CurrentRow("Location").ToString
Row("Inj") = CurrentRow("Inj").ToString

HeaderTable.Rows.Add(Row)

Case "data"

If DataTable Is Nothing Then
DataTable = New DataTable("DataTable")
''# Get number of columns
UpperBound = CurrentRow.GetUpperBound(0)
''# Create new DataTable
For ColumnCount = 0 To UpperBound
Column = New DataColumn()
Column.DataType = System.Type.GetType("System.String")
Column.ColumnName = "Column" & ColumnCount
Column.Caption = "Column" & ColumnCount
Column.ReadOnly = True
Column.Unique = False
DataTable.Columns.Add(Column)
Next
End If
Row = DataTable.NewRow
For ColumnCount = 0 To UpperBound
Row("Column" & ColumnCount) = CurrentRow(ColumnCount).ToString
Next
DataTable.Rows.Add(Row)
End Select
End If
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
TextFileReader.Dispose()

End Sub

Continue reading...
 
Back
Top