Last post Aug 28, 2011 12:42 AM by Decker Dong - MSFT
Aug 23, 2011 09:52 AM|therod0308|LINK
I am trying to read a CSV file into a gridview. I can see that both the column headers and data are being converted from the CSV file successfully because they are being displayed (unformatted) on the webpage. They just aren't being displayed in the gridview.
Can anyone see what I am doing wrong?
Dim GridView1 As New GridView
Dim TableName As String = "Options"
Dim CsvFile As String = "data/quotes.csv"
Dim DataPath As String = Server.MapPath(CsvFile)
Dim delimiter As String = ","
Dim ds As New DataSet()
' add GridView to page
' declare CSV parser passing in path to file
Dim sr = New StreamReader(DataPath)
' Split the 3rd line into the fields and add to string array called columns
Dim columns As String() = sr.ReadLine().Split(delimiter.ToCharArray())
' Cylcle the columns, adding those that do not exist yet and sequencing the ones that do
For Each col As String In columns
Dim added As Boolean = False
Dim _next As String = ""
Dim i As Integer = 0
While Not added
' Build the column name and remove unwanted characters
Dim columnname As String = col + _next
columnname = columnname.Replace("#", "")
columnname = columnname.Replace("'", "")
columnname = columnname.Replace("&", "")
columnname = columnname.Replace("""", "")
' See if the column already exists
If Not ds.Tables(TableName).Columns.Contains(columnname) Then
added = True
'Response.Write(columnname & "<br />")
' If it did exist then we increment the sequencer and try again
i = i + 1
_next = "_" + i.ToString()
' Read the rest of the data in the file
Dim allData As String = sr.ReadToEnd()
' Terminate each row at CRLF
Dim rows As String() = allData.Split(vbCr.ToCharArray)
' Add each row to the Dataset
For Each rowValue As String In rows
' Remove quotations if any
Dim row As String = rowValue.ToString().Replace("""", "")
' Use delimiter to split row into column data items
Dim items As String() = row.Split(delimiter.ToCharArray())
' Add the data item to the dataset
' clean up StreamReader
' tell GridView to create display based on values from DataSource
GridView1.DataSource = ds.Tables(TableName)
Aug 23, 2011 10:03 AM|Rajneesh Verma|LINK
Aug 23, 2011 10:40 AM|therod0308|LINK
Thanks for your response but the first article refers to exporting to a csv file; the second replicates what I've already accomplished; and the third is a bad link.
As I said in my original post, I am getting the data from the CSV file into the datasource successfully, however the datasource is not binding to the gridview. That's what I am trying to figure out.
Aug 24, 2011 11:05 PM|Decker Dong - MSFT|LINK
The third link is well, and I've pasted it for you to see:
void btn_ImportCSV_Click(object sender,
= "Please check the selected file type";
//Create data table columns for the first row is columns
for (int i
= 0; i
< columns.Count(); i++)
//Add row data
for (int i
= 1; i
< fileContent.Count(); i++)
Aug 25, 2011 12:34 PM|therod0308|LINK
I appreciate the time you took in posting a reply for my gridview issues. However, I don't want to rewrite the code. Like I said in my original post, I can see the results of the data in the CSV file being extracted and displayed on the .aspx page. My
question was, why I still can't get it to bind to the gridview.
Is there some secret I don't know about?
The data is actually displayed in a table correctly on the page. It just isn't in the gridview.
Aug 25, 2011 08:58 PM|Decker Dong - MSFT|LINK
I am getting the data from the CSV file into the datasource successfully, however the datasource is not binding to the gridview.
Not binding to the GridView? What result have you got?
And would you mind offering us your screenshot?
If possible, Plz offer us your whole project with csv file as well as your related aspx pages and cs code files to
email@example.com，With the title of the current URL issue.
Aug 25, 2011 09:21 PM|therod0308|LINK
Emailed the project to you. Thanks for going above and beyond.
Aug 27, 2011 03:26 AM|Decker Dong - MSFT|LINK
It's because you should comment "Dim GridView1 As New GridView". Because you've got another one inside the aspx page.
Aug 27, 2011 04:22 AM|therod0308|LINK
Thanks again Decker. Sometimes the bugs right in front of your eyes are the hardest to find. :)
Aug 28, 2011 12:42 AM|Decker Dong - MSFT|LINK
Hope you can come to the forum frequently and help more
Happy coding and happy sharing!