When I am reading a csv file to dataTable and populating a Datagridview I am getting
problem that one blank row inserted into datagridview why? what wrong with my code
please somebody help me. below my code

Private Sub ReadCsv()
Dim myTable As DataTable = New DataTable
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
'Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields there are.
myReader = IO.File.OpenText("C:\Test2.csv")
fieldValues = myReader.ReadLine().Split(",")
'Create data columns accordingly
' For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Name")) '("Field" & i))
myTable.Columns.Add(New DataColumn("Last Price"))
myTable.Columns.Add(New DataColumn("Date"))
myTable.Columns.Add(New DataColumn("Time"))
myTable.Columns.Add(New DataColumn("Change in RS"))
myTable.Columns.Add(New DataColumn("Open"))
myTable.Columns.Add(New DataColumn("High"))
myTable.Columns.Add(New DataColumn("Low"))
myTable.Columns.Add(New DataColumn("Vol"))
' Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(",")
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return 'New DataTable("Empty")
Finally
myReader.Close()
End Try
DataGridView2.DataSource = myTable


Return 'myTable
End Sub

See this thread >>> http://www.daniweb.com/software-development/vbnet/threads/380681

I have seen ur post, look at the picture what u have upload that is also same problem below the datagridview one extra blank row also created, actually what is happening when I am trying to change the color of datagridview comparing between two cells the code is below
-----------------------------------------------------------
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Cells(1).Value >= DataGridView1.Rows(i).Cells(6).Value Then
DataGridView1.Rows(i).Cells(1).Style.BackColor = Color.Green
ElseIf DataGridView1.Rows(i).Cells(1).Value <= DataGridView1.Rows(i).Cells(6).Value Then
DataGridView1.Rows(i).Cells(1).Style.BackColor = Color.Red

End If
Next
-----------------------------------------------------------------
I am getting error (Operator '>' is not defined for type 'DBNull' and type 'DBNull'.)

The only way I know not to have that extra row is to set the "AllowUserToAddRows" Property to False.

The only way I know not to have that extra row is to set the "AllowUserToAddRows" Property to False.

I was reexamining my project/post. I found that there was a blank line at the end of my CSV file. When I deleted all blanks back to the last character of the file the Blank Row in the DataGridView was gone. Also there was the row that the DataGridView places there by default using the the setting in my previous post should get rid of all blank rows.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.