Link to home
Start Free TrialLog in
Avatar of byte1
byte1Flag for United States of America

asked on

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
Avatar of HainKurt
HainKurt
Flag of Canada image

check this

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

you can read it like a table...
Avatar of Dmitry G
By the way, does your original file has new lines after each data row?
Avatar of byte1

ASKER

yes it does.
Avatar of byte1

ASKER

Both Examplles were the same, the problem is my CSV formatted text is in a string variable...
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of byte1

ASKER

@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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
Avatar of byte1

ASKER

@Hainkurt: Sorry i don't quite understand your logic to split the lines twice ?
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

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

Avatar of byte1

ASKER

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.
Avatar of byte1

ASKER

DataGrid1.DataSource = mydatatable
then use

DataGrid1.DataBind()