Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

import csv file into datagrid - Xtreme Visual Basic Talk http://www.xtremevbtalk.com/showthread.php?

t=251869

User Name Remember Me?


Password

Home Register Calendar Today's Posts FAQ Free Publications Search

Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > import csv file into datagrid

Advertisement:

Thread Tools Display Modes

01-25-2006, 09:18 PM #1

chngbh Join Date: Mar 2005


Centurion Posts: 169

import csv file into datagrid

Can i import csv file into datagrid? Please advice on the following
code?

Code:

'capture filename in reserve


Dim n, i As Integer
For n = chkpoint - 2 To 0 Step -1
If desc.Substring(n, 1) <> "\" Then
filename &= desc.Substring(n, 1)
Else
Exit For
End If
Next

'reserve back the filename


n = filename.Length
desc = ""

For i = n - 1 To 0 Step -1
desc &= filename.Trim.Substring(i, 1)
Next

Dim strSQLdg As String

strSQLdg = "Provider=Microsoft.Jet.Oledb.4.0;data source=" & txtPath.Text & _


";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
myCon = New System.Data.OleDb.OleDbConnection(strSQLdg)

'select sheet1 to view


myAdp = New System.Data.OleDb.OleDbDataAdapter( _
"select * from " & desc.Trim, myCon)

DS = New DataSet
DS.Clear()
myAdp.Fill(DS)

Last edited by wayneph; 01-26-2006 at 12:20 PM. Reason: please use [vb] tags for posting code

01-26-2006, 12:23 PM #2

wayneph Join Date: Apr 2004


Web Junkie Location: D/FW, Texas, USA
Posts: 8,393

Retired Moderator
* Expert *

Take a look here: http://weblogs.asp.net/fmarguerie/ar.../01/29964.aspx

(The first thing I notice is your Connection String Is pointed to Excel, not Jet...)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned

02-08-2006, 02:45 AM #3

1 of 4 26/01/2013 13:04
import csv file into datagrid - Xtreme Visual Basic Talk http://www.xtremevbtalk.com/showthread.php?t=251869

chngbh Join Date: Mar 2005


Centurion Posts: 169

Thanks for your reply, i had change my coding that can accept csv file format, as below

strSQLdg = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath.Trim & ";Extended Properties=""text;


HDR=Yes;FMT=Delimited""" 'csv format
myCon = New System.Data.OleDb.OleDbConnection(strSQLdg)

'select sheet1 to view


myAdp = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [" & strfilename & "]", myCon)

but i faced another problem, for example as below

____col1 col2
row1 1 K456K021-02-1
row2 2 846548621

so as usual, i try to display the data into datagrid but the (row1,col2) data display as null.... do you know how to
solve this problem
please advice.... your comment is very valuable to me
thanks

02-08-2006, 08:53 AM #4

wayneph Join Date: Apr 2004


Web Junkie Location: D/FW, Texas, USA
Posts: 8,393

Retired Moderator
* Expert *

it looks like it's "assuming" that column two is an integer. since it contains letters it is failing. Try declaring your
table first with the second column declared as a string. see if that helps.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned

02-08-2006, 10:25 AM #5

Join Date: Aug 2005


silenuz Location: Ottawa
Junior Contributor Posts: 203

You might want to try the TextFieldParser.


Code:
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser _
("C:\TestFolder\test.txt")

MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")

Dim currentRow As String()


While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
MsgBox(currentField)
Next
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
End Using

You might also want to check out:


http://msdn2.microsoft.com/en-us/library/2ewecs64.aspx

02-08-2006, 09:52 PM #6

chngbh Join Date: Mar 2005


Centurion Posts: 169

wayneph,
how do i declare the column two as string using VB .Net? but i had try to do it in excel by changing the format
cell into Text/General... but i still read it as null

silenuz,
thanks for you code, but i think your code capable under VB but not in VB .Net

02-09-2006, 08:48 AM #7

Join Date: Aug 2005


silenuz Location: Ottawa
Junior Contributor Posts: 203

2 of 4 26/01/2013 13:04
import csv file into datagrid - Xtreme Visual Basic Talk http://www.xtremevbtalk.com/showthread.php?t=251869

No it's VB . Net. See


http://msdn2.microsoft.com/en-us/lib...US,VS.80).aspx
http://msdn2.microsoft.com/en-us/library/x710fk43.aspx
Although I'm not sure if it's in VS 2003 but it is in 2005. It should be noted too that the code example just
shows each field in a messagebox, you'd want to change that to something like the following:
Code:
'change the filepath to the file you wish to access
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser _
("K:\MSVisual 2005\WindowsApplication1\WindowsApplication1\document.txt")

MyReader.TextFieldType = FileIO.FieldType.Delimited

'my test file used semicolon seperated values change to comma


'or whatever the delimeter is
MyReader.SetDelimiters(";")

Dim currentRow As String()

Dim currentField As String

Dim i As Integer = 0

currentRow = MyReader.ReadFields()

'here you'd probably want to create actual column names for instead
'of just fieldi
For Each currentField In currentRow
i += 1
'the first "Field" & i.tostring sets column name, the second
'the column header
DataGridView1.Columns.Add("Field" & i.tostring, "Field" & i.tostring)
Next

While Not MyReader.EndOfData


Try
currentRow = MyReader.ReadFields()
DataGridView1.Rows.Add(currentRow)

Last edited by silenuz; 02-09-2006 at 10:21 AM.

02-09-2006, 09:27 AM #8

wayneph Join Date: Apr 2004


Web Junkie Location: D/FW, Texas, USA
Posts: 8,393

Retired Moderator
* Expert *

For mine, you need to create the DataTable that you Fill from the DataAdaper up front.

This will show you how to create the Columns: http://msdn.microsoft.com/library/en...odatatable.asp

You can also add ColumnMappings that will help make sure that the data ends up in the right place:
http://msdn.microsoft.com/library/en...mnmappings.asp

(Once in one of these MSDN links, you can use the categories they're in to get a ton more information.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned

02-09-2006, 08:08 PM #9

chngbh Join Date: Mar 2005


Centurion Posts: 169

silenuz,
i had found out that your coding is under VS2005 with .NET framework version 2... but your code can be used if
i install framework version 2... by the way, can framework version 2 support VS2003?

wayneph,
thanks for your advice

02-09-2006, 08:39 PM #10

Join Date: Aug 2005


silenuz Location: Ottawa
Junior Contributor Posts: 203

I'm pretty sure there is no support in 2003 for .Net V2 but with MS giving away copies of the express editions of
VS 2005 you could always download it and try it. It runs side by side with 2003, but I loing since uninstalled
2003, never really used it anyway,waste of company expenses as I held out using VB6 till a few months ago,
and I figured if I was finally going to take the .Net plunge I would just start with 2005.

Last edited by silenuz; 02-09-2006 at 09:01 PM.

02-15-2006, 10:04 PM #11

chngbh Join Date: Mar 2005


Centurion Posts: 169

3 of 4 26/01/2013 13:04
import csv file into datagrid - Xtreme Visual Basic Talk http://www.xtremevbtalk.com/showthread.php?t=251869

Quote:

Originally Posted by wayneph


For mine, you need to create the DataTable that you Fill from the DataAdaper up front.

This will show you how to create the Columns: http://msdn.microsoft.com/library/en...odatatable.asp

You can also add ColumnMappings that will help make sure that the data ends up in the right place:
http://msdn.microsoft.com/library/en...mnmappings.asp

(Once in one of these MSDN links, you can use the categories they're in to get a ton more information.)

wayneph,
i had create the datatable but how can i pass the data into it while the csv file still haven't read... once it read,
the value that i wants had become null...
as i know that we need to assign the data into the datatable...
how can i pass the data direct from csv into the datatable?

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

Posting Rules

You may not post new threads


You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Forum Rules

Free Publications

.Net Insight eNewsletter Modern Masters Art


Takes you through the maze of Visual Studio and .NET technologies, from development tools and Original Fine Art. Picasso,Chagall, Miro and
platforms to hosted Web services and migration strategies. more
subscribe clarkfineart.com

The ASP.NET 2.0 Anthology WinForms OLAP Control


101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best Pivot DataGrid for Windows Forms Fast and
approaches for ASP.NET. Easy to use. Free Trial!
subscribe www.viblend.com

Programmers Heaven C# School Book -Free 338 Page eBook Japan Anime Tours
The Programmers Heaven C# School book covers the .NET framework and the C# language. Tour major anime attractions for the ultimate otaku
subscribe experience!
www.inspiredsteps.o rg/anime

Microsoft 70-305 Developing and Implementing Web Apps with VB .NET and VS .NET SE Special
Edition Practice Exam Kaos tentang Indonesia
Bahan Katun Enak, Nasionalis Gaul dari
ExamForce practice exams save IT professional's time and money by focusing their study time on the Kementerian Desain R.I
areas they need help most. distrokdri.com
subscribe

Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and
running in no time..
subscribe

Visual SQL to XML


www.ecrio n.com
Easy to use Data Mapping enviro nment. Try no w!

Home Register Calendar Today's Posts FAQ Archive

© Xtremevbtalk.com 2001 - 2013. All Rights Reserved.

Powered by vBulletin® Version 3.8.6


Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

-->

4 of 4 26/01/2013 13:04

You might also like