Results 1 to 20 of 20

Thread: CSV to a form ??? please help .... [resolved]

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved 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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim SR As StreamReader = IO.File.OpenText("badger.csv")
    3.         Dim strFileString As String
    4.         Dim al As New ArrayList
    5.         Dim filetext() As String
    6.         Dim i, j As Integer
    7.  
    8.         Dim dt As New DataTable
    9.         Dim dr As DataRow
    10.  
    11.         Do While SR.Peek <> -1
    12.             al.Add(SR.ReadLine)
    13.  
    14.         Loop
    15.  
    16.         filetext = Split(al(0), ",")
    17.         For j = 0 To filetext.Length - 1
    18.             ListBox1.Items.Add(filetext(j))
    19.             dt.Columns.Add(filetext(j))
    20.         Next
    21.  
    22.         For i = 1 To al.Count - 1
    23.             Dim r As DataRow = dt.NewRow
    24.             filetext = Split(al(i), ",")
    25.             For j = 0 To filetext.Length - 1
    26.  
    27.                 'ListBox1.Items.Add(filetext(j))
    28.                 r(j) = filetext(j)
    29.             Next
    30.             dt.Rows.Add(r)
    31.             '  ListBox1.Items.Add(filetext(3).ToString)
    32.         Next
    33.  
    34.         DataGrid1.DataSource = dt
    35.     End Sub

    Any ideas folks ?
    Attached Images Attached Images  
    Last edited by dinosaur_uk; Nov 5th, 2004 at 07:46 AM.

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Hello, yes, it's a heck of a lot easier than that

    Before you begin, you have to realize that Microsoft in it's less than infinite wizdom regocnizes that any structured storage system is a potential data source. Bearing that in mind, realize that a csv file is a structured storage source, and because it is, we can open it with ADO.

    I recoded your routine to make use of a few ADO objects and now it works like this:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim DataFile As FileInfo = New FileInfo("C:\Temp\badger.csv")
    3.         Dim cnCSV As OdbcConnection
    4.         Dim daCSV As OdbcDataAdapter
    5.         Dim dt As DataTable = New DataTable("badger")
    6.  
    7.         cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")
    8.         daCSV = New OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", Conn)
    9.         daCSV.Fill(dt)
    10.         DataGrid1.DataSource = dt
    11.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    What is ADO ? i am so sorry for these silly questions...

    is it Advanced Data Objects ?

    Cheers ...

    Will try it out now

    you guys are beautiful people

  4. #4

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    Apparently i need to define the
    OdbcConnection
    OdbcDataAdapter

    etc

    how do i define them ?

    Sorry for the stupid questions

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Howdy,

    You need to add an imports statement at the top of your module:

    Imports System.Data.Odbc

    ADO is ActiveX Data Objects, which isn't appropriately named anymore because the .NET framework isn't based on com or ActiveX.

    so it's really ADO.NET, Come on Micro$oft, give it a new name.
    Whadayamean it doesn't work....
    It works fine on my machine!

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    Now i need to declare conn, what do i need to put for the connection string ?

  7. #7

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    I get this error if i just run it anyway...


    An unhandled exception of type 'System.Data.Odbc.OdbcException' occurred in system.data.dll

    Additional information: System error.

  8. #8
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Hey, do me a favor, let me know if you have an Instant Messaging client, AOL, Yahoo, MSIM?
    Whadayamean it doesn't work....
    It works fine on my machine!

  9. #9

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    I do have Instant messenger, [email protected] but i cant access it at work ....

  10. #10
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    If you carefully review my code you will see that every object you need to open the data and connect it to your grid is coded. You only need to change the file location for where your file is located and then it will work. Would you like me to send you an example project?
    Whadayamean it doesn't work....
    It works fine on my machine!

  11. #11

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    Yes please, that would be amazing !!!!

  12. #12
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim DataFile As FileInfo = New FileInfo("C:\Temp\badger.csv")
    3.         Dim cnCSV As OdbcConnection
    4.         Dim daCSV As OdbcDataAdapter
    5.         Dim dt As DataTable = New DataTable("badger")
    6.  
    7.         cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")
    8.         [COLOR=red]daCSV = New OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", cnCSV)[/COLOR]
    9.         daCSV.Fill(dt)
    10.         DataGrid1.DataSource = dt
    11.     End Sub
    I'll send you an example project in a few minutes.
    Whadayamean it doesn't work....
    It works fine on my machine!

  13. #13

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098
    It works !!!!
    Oh yes! !!!

    you are a beautiful person !!!!!!

  14. #14
    New Member
    Join Date
    Jun 2006
    Posts
    1

    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.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  16. #16
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477

    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.
    Whadayamean it doesn't work....
    It works fine on my machine!

  17. #17
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: CSV to a form ??? please help .... [resolved]

    Or you can check out the CSV and TAB file parser codebank submission in my sig

  18. #18
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477

    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.
    Whadayamean it doesn't work....
    It works fine on my machine!

  19. #19
    Junior Member
    Join Date
    Jun 2006
    Posts
    22

    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

  20. #20
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim DataFile As FileInfo = New FileInfo("C:\Temp\badger.csv")
    3.         Dim cnCSV As OdbcConnection
    4.         Dim daCSV As OdbcDataAdapter
    5.         Dim dt As DataTable = New DataTable("badger")
    6.  
    7.         cnCSV = New OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & DataFile.Directory.FullName & ";")
    8.         [COLOR=red]daCSV = New OdbcDataAdapter("SELECT * FROM [" & DataFile.Name & "]", cnCSV)[/COLOR]
    9.         daCSV.Fill(dt)
    10.         DataGrid1.DataSource = dt
    11.     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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width