Display csv data in VB.net

  • Thread starter Thread starter Microsoft
  • Start date Start date
M

Microsoft

I have a program that creates out put in the form of a CSV.. Basically a
text file with entries that look like this 1,2,3,4

Is there a control or way that I could display the information in a nice
table format directly on my VB form, without having to have Excel open a
file .. Thanks!
 
Microsoft said:
I have a program that creates out put in the form of a CSV..
Basically a text file with entries that look like this 1,2,3,4

Take a look at <URL:http://www.connectionstrings.com/>. You'll find
connection strings for accessing CSV files in the "Text" section there. If
you have read the data into a dataset, you can bind it to a datagrid, for
example.
 
I'll be interested to see how others answer this but here's my method.

'Open a connection using the Jet engine telling it to work with a text
file.

Dim cnText As New
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\MyData;Extended Properties=Text")

'Then read it with a data adapter or data reader.

cnText.Open()
Dim cmdTextSelect As New System.Data.OleDb.OleDbCommand("Select * from
MyFile.txt", cnText)
Dim drText As OleDbDataReader = cmdTextSelect.ExecuteReader()


You'll need a schema.ini file to define the text file. See this MSDN page.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcjetschema_ini_file.asp

And BTW if you want text instead of numbers you many find this
undocumented parameter useful.

TextDelimiter='


Tom
 
¤ I have a program that creates out put in the form of a CSV.. Basically a
¤ text file with entries that look like this 1,2,3,4
¤
¤ Is there a control or way that I could display the information in a nice
¤ table format directly on my VB form, without having to have Excel open a
¤ file .. Thanks!
¤

You can use a DataGrid and ADO.NET:

Dim ConnectionString As String
Dim SQLString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\TextFiles;" & _
"Extended Properties=""Text;HDR=NO;"""

SQLString = "Select * from ReportFile.txt"

Dim ConnectionText As New OleDb.OleDbConnection
ConnectionText.ConnectionString = ConnectionString

ConnectionText.Open()

Dim AdapterText As New OleDb.OleDbDataAdapter(SQLString, ConnectionText)

Dim DataSetText As New DataSet("TextFiles")
AdapterText.Fill(DataSetText, "TextFile")
DataGrid1.SetDataBinding(DataSetText, "TextFile")

ConnectionText.Close()


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
If there isnt too much data you could
read the file into a string,
call Split on it to build an array
copy the array into the display control of your choice - combo, listbox or
wahtever

hth

guy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top