Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

CSV to DataGrid

How do i display a CSV formatted file in a datagrid ?

my string looks like this

Server,Backup Set,Directory, Size lnations_calserver2,CALSERVER2_SQL,///AdvancedMSSQLServer///,27405648 lnations_calserver2,Cdrive,C:/ComplianceOne_backups,394181048 lnations_calserver2,Cdrive,C:/Program Files/ComplianceOne,206285308 lnations_calserver2,SysState,///SystemState///,513046208
0
byte1
Asked:
byte1
  • 6
  • 5
  • 5
2 Solutions
 
HainKurtSr. System AnalystCommented:
check this

http://forums.devx.com/showthread.php?t=148498

you can read it like a table...
0
 
anarki_jimbelCommented:
0
 
anarki_jimbelCommented:
By the way, does your original file has new lines after each data row?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
byte1Author Commented:
yes it does.
0
 
byte1Author Commented:
Both Examplles were the same, the problem is my CSV formatted text is in a string variable...
0
 
HainKurtSr. System AnalystCommented:
then do this

create a table, add columns
lines = scv.split(chrEOL)

for each l in lines
  v = l.split(",")
  r = new tbl.datarow
  for i = 0 to 10 r(i)=v(i)
  tbl.add(r)
next

now you can bind this to your grid...
0
 
anarki_jimbelCommented:
OK, if the file contains new lines, why doesn't the string?
How many columns you have in the cvs table - I'm not quite sure?

HainKurt ideais good enough, I believe.
0
 
byte1Author Commented:
@anarki : The String variable just has contents from a CSV file. If i write it to a file it would conform to CSV file specifications.
0
 
anarki_jimbelCommented:
byte1, the solution from HainKurt supposes splitting by a new line charactyer. Your string does not have new line characters, therefore the above solution will not work. I believe the string you gave her may not exactly correct.
If we know number of fields we may use this info and forget about new lines...

I believe the string you gave should look like the one below (with new lines - like any other csv):

Server,Backup Set,Directory, Size
lnations_calserver2, CALSERVER2_SQL, ///AdvancedMSSQLServer///,27405648
lnations_calserver2, Cdrive,C:/ComplianceOne_backups,394181048
lnations_calserver2, Cdrive,C:/Program Files/ComplianceOne,206285308
lnations_calserver2, SysState,///SystemState///,513046208

And, of course, it has 4 fields...
0
 
anarki_jimbelCommented:
One possible guess: when text was read from a file with 'ReadLine" statement new line characters were not added between consequent lines in the resulting string...
0
 
byte1Author Commented:
@Hainkurt: Sorry i don't quite understand your logic to split the lines twice ?
0
 
HainKurtSr. System AnalystCommented:
I thought you have the text file in a string with EOL chars in it...
thats why firs I split into lines, and loop all lines, split each line into columns...

if you have a file then the logic should be:
create a table, add columns
myfile = "c:\..\..\some.csv"
open(myfile)

while not eof(myfile)
  l = ReadLine(myfile) ' get a line
  v = l.split(",") ' split the line to columns
  r = new tbl.datarow ' create a row to hold the line values
  for i = 0 to 10 r(i)=v(i) ' put all values into the new row
  tbl.add(r) ' add new row to the table
loop

now you can bind this to your grid... 

Open in new window

0
 
HainKurtSr. System AnalystCommented:
but if you have file the link should work fine for you...
here is the sample from first link I sent you...
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()

Open in new window

0
 
byte1Author Commented:
The earlier sample did work for me, I was able to split the lines properly, no problem there. However I am not able to bind the table to the datagrid.
0
 
byte1Author Commented:
DataGrid1.DataSource = mydatatable
0
 
HainKurtSr. System AnalystCommented:
then use

DataGrid1.DataBind()
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now