byte1
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,CALSER VER2_SQL,/ //Advanced MSSQLServe r///,27405 648 lnations_calserver2,Cdrive ,C:/Compli anceOne_ba ckups,3941 81048 lnations_calserver2,Cdrive ,C:/Progra m Files/ComplianceOne,206285 308 lnations_calserver2,SysSta te,///Syst emState/// ,513046208
my string looks like this
Server,Backup Set,Directory, Size lnations_calserver2,CALSER
By the way, does your original file has new lines after each data row?
ASKER
yes it does.
ASKER
Both Examplles were the same, the problem is my CSV formatted text is in a string variable...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
How many columns you have in the cvs table - I'm not quite sure?
HainKurt ideais good enough, I believe.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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:
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...
but if you have file the link should work fine for you...
here is the sample from first link I sent 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()
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.
ASKER
DataGrid1.DataSource = mydatatable
then use
DataGrid1.DataBind()
DataGrid1.DataBind()
http://forums.devx.com/showthread.php?t=148498
you can read it like a table...