load .csv data to a datagrid

W

with a question

Hi this is the code i have written.
my csv file looks lke this
Column1,Column2,Column3
1,1,1
43,43,4
132,234,243
54,53,46

but still i get only
null null null
null null null
null null null
in my datagrid - what should i do.

Dim cn As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\;Extended Properties=""Text;HDR=No;FMT=Delimited""")


Dim da As New OleDbDataAdapter

Dim ds As New DataSet
Dim cd As New OleDbCommand("SELECT * FROM C:\Test1.csv", cn)
cn.Open()
da.SelectCommand = cd
ds.Clear()
da.Fill(ds, "CSV")
DataGrid1.DataSource = ds.Tables(0)
cn.Close()
 
J

james

with a question said:
Hi this is the code i have written.
my csv file looks lke this
Column1,Column2,Column3
1,1,1
43,43,4
132,234,243
54,53,46

but still i get only
null null null
null null null
null null null
in my datagrid - what should i do.

Dim cn As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\;Extended Properties=""Text;HDR=No;FMT=Delimited""")


Dim da As New OleDbDataAdapter

Dim ds As New DataSet
Dim cd As New OleDbCommand("SELECT * FROM C:\Test1.csv", cn)
cn.Open()
da.SelectCommand = cd
ds.Clear()
da.Fill(ds, "CSV")
DataGrid1.DataSource = ds.Tables(0)
cn.Close()

I would change two things:
where you have HDR=No, I would change it to : Yes ( you have column names in
your data you are showing here)
Second change:
Datagrid1.DataSource = ds.Tables(0)
to:
Datagrid1.Datasource = ds.Tables(0).DefaultView

That should get you going.
james
 
S

Scott

God I love newsgroups. I've spent 10+ hours searching for how to do just
this. Then I remember newsgroups and bingo - I'm in.

Thanks guys.

(Been a while since I picked up VB and now it's VB.net and I'm just flailing
madly through).
 

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

Top