CSV file to DataTable to DataGridView Issue

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

Hey All,

I have a .csv file that I am dumping into a DataTable and then using it to
populate a DataGridView control.

It appears that my DataTable is recognizing postal codes as integers and
when it gets one like 14895-3456, for example, it wipes it out because it is
showing up blank in the DataGridView.

Is there a quick way such that I can get everything the data represented as
strings and, thus, not get cleared?

Thanks,

Todd
 
Todd,

I think you can set the valuetype of the zip column:

datagridview.Columns(x).ValueType = GetType(System.String)

Rick
 
¤ Hey All,
¤
¤ I have a .csv file that I am dumping into a DataTable and then using it to
¤ populate a DataGridView control.
¤
¤ It appears that my DataTable is recognizing postal codes as integers and
¤ when it gets one like 14895-3456, for example, it wipes it out because it is
¤ showing up blank in the DataGridView.
¤
¤ Is there a quick way such that I can get everything the data represented as
¤ strings and, thus, not get cleared?

You may need to define the data types for your CSV file using a schema.ini file, or the Text ISAM
driver will simply guess as to the data types based upon the data present in each column.

http://msdn.microsoft.com/en-us/library/ms709353.aspx


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Hey Paul,

OK. This is a problem because in this application, users select the file
using a 'FileOpen' dialog (i.e. the filename can be anything).

Should I be using a different method to read these files?

Thanks,

Todd
 
¤
¤ Hey Paul,
¤
¤ OK. This is a problem because in this application, users select the file
¤ using a 'FileOpen' dialog (i.e. the filename can be anything).
¤
¤ Should I be using a different method to read these files?
¤

You could try using the TextFieldParser class instead:

Dim TextFileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Documents and
Settings\...\My Documents\My Database\Text\Orders.txt")

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

Dim CurrentRow As String()
While Not TextFileReader.EndOfData
Try
CurrentRow = TextFileReader.ReadFields()
Dim CurrentField As String
For Each CurrentField In CurrentRow
Console.Write(CurrentField & Space(1))
Next
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
Console.WriteLine()
End While


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top