-
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
-
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!
-
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
-
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!
-
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
-
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
-
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
-
By software_develo in forum Database
Replies: 2
Last Post: 11-21-2005, 10:18 AM
-
By anshulsarabhai in forum Database
Replies: 0
Last Post: 05-24-2005, 08:23 AM
-
By jase_dukerider in forum C++
Replies: 2
Last Post: 04-14-2005, 07:48 PM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Dana in forum Database
Replies: 3
Last Post: 10-09-2000, 05: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks