1 Attachment(s)
CSV to a form ??? please help .... [resolved]
Hi there,
I have a csv file which i would like to display in a control on my form which behaves like excel spreadsheet.
here is a screenshot of the datatable which i use to put the data on. Is there a more efficient way of doing this ?
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SR As StreamReader = IO.File.OpenText("badger.csv")
Dim strFileString As String
Dim al As New ArrayList
Dim filetext() As String
Dim i, j As Integer
Dim dt As New DataTable
Dim dr As DataRow
Do While SR.Peek <> -1
al.Add(SR.ReadLine)
Loop
filetext = Split(al(0), ",")
For j = 0 To filetext.Length - 1
ListBox1.Items.Add(filetext(j))
dt.Columns.Add(filetext(j))
Next
For i = 1 To al.Count - 1
Dim r As DataRow = dt.NewRow
filetext = Split(al(i), ",")
For j = 0 To filetext.Length - 1
'ListBox1.Items.Add(filetext(j))
r(j) = filetext(j)
Next
dt.Rows.Add(r)
' ListBox1.Items.Add(filetext(3).ToString)
Next
DataGrid1.DataSource = dt
End Sub
Any ideas folks ?
Re: CSV to a form ??? please help .... [resolved]
I had a quick question cyberhawke, would it be possible to incorporat a WHERE statement into the "OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", cnCSV)" line? I am trying to make a program that will select from live data and I want to be able to give the program a reference to search for. Would that be possible using this method at all? If not how else could I do it. Thanks a bunch for your help!!!!
P.S. I have not written VB in years and I have very limited knowledge sorry.
Re: CSV to a form ??? please help .... [resolved]
This thread is quite old, so I dont know if CyberHawke will see it.. you should be able to just add a Where clause after the ] tho, as that is the end of the SQL statement.
Re: CSV to a form ??? please help .... [resolved]
An easier route would be to open the dataset as shown, and then build a set of datarows from the table in the opened dataset using the DataTable.Select method.
Re: CSV to a form ??? please help .... [resolved]
Or you can check out the CSV and TAB file parser codebank submission in my sig :D
Re: CSV to a form ??? please help .... [resolved]
File parsers are great if you want to look at the text in a file, but not very useful if you actually want to use the data in the CSV as data in a table driven system. Depends on the need of your system. :afrog:
Re: CSV to a form ??? please help .... [resolved]
Hello CiberHawke
your code runs great, with a few modifications to run it in VB 2005 express, it's very good. The only thing is that I don't get the parsed fields in the datagrid, everything gets in the first (and only) column. Was this code supposed to do the parsing, or do I have to add some code? I'm kind of lost here.
Thanks
Re: CSV to a form ??? please help .... [resolved]
Quote:
Originally Posted by CyberHawke
Actually, as I was reviewing my post, I realized that I left a piece of code in there by mistake. Here is the corrected code:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DataFile As FileInfo = New FileInfo("C:\Temp\badger.csv")
Dim cnCSV As OdbcConnection
Dim daCSV As OdbcDataAdapter
Dim dt As DataTable = New DataTable("badger")
cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")
[COLOR=red]daCSV = New OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", cnCSV)[/COLOR]
daCSV.Fill(dt)
DataGrid1.DataSource = dt
End Sub
I'll send you an example project in a few minutes.
How would i change this so that the first row did not become the collum names
( bouns question) how would make my own collum names if i knew they were static, with out altering the csv file?