Importing CSV file to data grid


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Importing CSV file to data grid

  1. #1
    Join Date
    Oct 2005
    Posts
    95

    Importing CSV file to data grid

    Hi

    I like to able to import a CSV file to the data grid . My code is working and importing the csv file to the data grid . BUT it only import the file I have specified in the program ( test.csv ). I need to give user a option to choose a CSV file they want from the directory and import that when they click on the import button . So when a user click on the import button on the form it will open the directory structure and user will able to choos the csv file and click import to import that choosen CSV file to the data grid . Please help me to modify my code . My code is below


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


    Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;"

    Dim objConn As New OleDbConnection(sConnectionString)

    objConn.Open()

    Dim objCmdSelect As New OleDbCommand("SELECT * FROM test.csv", objConn)

    Dim objAdapter1 As New OleDbDataAdapter

    objAdapter1.SelectCommand = objCmdSelect

    Dim objDataset1 As New DataSet

    objAdapter1.Fill(objDataset1, "test")

    DataGrid1.DataSource = objDataset1.Tables(0).DefaultView

    objConn.Close()


    End Sub

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Add an OpenFileDialog control to your form, then change your code as follows:
    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
    
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Dim fi As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
            Dim objConn As New OleDbConnection(sConnectionString)
            objConn.Open()
            Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & fi.Name, objConn)
            Dim objAdapter1 As New OleDbDataAdapter
            objAdapter1.SelectCommand = objCmdSelect
            Dim objDataset1 As New DataSet
            objAdapter1.Fill(objDataset1, "test")
            DataGrid1.DataSource = objDataset1.Tables(0).DefaultView
            objConn.Close()
        End If
    
    End Sub
    If you don't already have "Imports System.IO" at the top of your code, you should add it as well.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Oct 2005
    Posts
    95

    CVS to Data Grid Import

    Hi

    Thanks so much for your help . It worked fine . could you please tell me how do I save this data grid information that I imported from the CSV file to the SQL server table . Also Is there a possibility to use SQL data adapter ( instead of OLEdb ) to import the CSV file to the .NET data Grid with vb.net .

    Thanks

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    This sample demonstrates how to update a database from a DataSet: http://samples.gotdotnet.com/quickst...ataFromDB.aspx

    I don't know of any way to use a SqlDataAdapter to import a CSV file.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Aug 2006
    Posts
    3

    Question help me please, simular issue to starting question!

    I have been trying to import a csv file into a datagraid. I tried the code that 'software_develo' used to make his work; however when i put it in a form load event (unchanged), it underlines 'OleDbConnection', 'OleDbCommand', 'OleDbDataAdapter' and does not work. I am making this vb application in vb .net

    I need to know how to make the form load a csv file into a datagrid on load event. I assumed thqat if i used this code in my load event rather than his button click it'll work. I was wrong!

    If any one can help then please let me know asap as i have 2 weeks to finnish it all. And that isn't long!

    This is what i have got:

    *frmMain - (The form with the DataGrid1 in)
    *DataGrid1 - (The grid that i am trying to import into)
    *tbMain - (Is a menu with file/mode/help etc...)

    Please somebody asap!

    Thanks in advanced, Shane Simpkins

  6. #6
    Join Date
    Aug 2006
    Posts
    3

    Question ok, i done it but....

    Ok thanks to people emailign em and stuff and me fiddling around i now have it importing perfectlyish. lol I however need to set thge headings(i believe in the file) and this is the hard bit. I need to be able to have a form that will allow me to add records and delete records. any ideas? I have done it with vba where it was basically macros and was able to read, delete, and add. Can anyone come to my rescue! Thanks

    Thanks in advanced, Shane Simpkins

  7. #7
    Join Date
    Aug 2006
    Posts
    3

    Question This is the code i have been trying to get working...

    Private Sub btnAddRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
    Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Stealth\My Documents\SchoolWork\GCSE_ICT\Task2\CalcSystem\;Extended Properties=Text;"
    Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
    objConn.Open()

    Dim ExportCommand As New System.Data.OleDb.OleDbCommand("INSERT * INTO [Text;DATABASE=C:\Documents and Settings\Stealth\My Documents\SchoolWork\GCSE_ICT\Task2\CalcSystem].[Income.csv] FROM" + DataGrid1.DataSource)
    ExportCommand.ExecuteNonQuery()

    objConn.Close()
    End Sub

    My aim is to import into a datagrid as i have and then you can add rows of dat into the datagrid; However i then want to save the datagrid source/text into a csv file. unless i can just siply update a csv file. If anyone can help please help! Thanks iun advanced!

Similar Threads

  1. Importing .NET data Grid to the SQL server database
    By software_develo in forum Database
    Replies: 2
    Last Post: 11-21-2005, 11:18 AM
  2. load data from .txt file to db2
    By anshulsarabhai in forum Database
    Replies: 0
    Last Post: 05-24-2005, 09:23 AM
  3. wav file fade in out help
    By jase_dukerider in forum C++
    Replies: 2
    Last Post: 04-14-2005, 08:48 PM
  4. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 11:40 PM
  5. Importing data.. sigh
    By Dana in forum Database
    Replies: 3
    Last Post: 10-09-2000, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center