Read CSV Data into textbox
Hello,
I want to be able to load & read (and later edit) .csv files into a textbox in Visual Basic 2008, If possible.
I don't know anything about this at all, but i do know a quite a bit about visual basic..
You would normally post the code you already have around about now, but i don't have any.. yet :(
Any help/code would be really appreciate..
Thanks!
Re: Read CSV Data into textbox
Welcome to VBForums :wave:
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
Re: Read CSV Data into textbox
If you simply want the entire file contents in a textbox, you can do this:
Code:
TextBox1.Text = System.IO.File.ReadAllText("C:\Temp\file.csv")
If you want more control over how the data is processed, then use the TextFieldParser
Re: Read CSV Data into textbox
Re: Read CSV Data into textbox
Okay, i have this:
Code:
Dim sr As New StreamReader("C:\Users\Luke\Desktop\Test1.csv")
Dim linesInFile As Integer
Do Until sr.EndOfStream = True
linesInFile = linesInFile + 1
TextBox1.Text = TextBox1.Text + sr.ReadLine & vbNewLine
Loop
That brings up Test1.csv into Textbox1... But how would i know separate the data. I know it is using the Split() command but i don't know how to use it..
So lets say inside the csv i have:
Bob,Jones,bob@hotmail.com,48
it is formatted there:
first name,second name,email,age
if i have 4 textboxes, how could i get all the information in a different textbox..
textbox1 would contain firstname
textbox2 would contain secondname
etc?
Re: Read CSV Data into textbox
It is fairly simple, one line of code to populate the TextBox
This example populates a TextBox where the data format is shown below the code.
Code:
TextBox1.Lines = _
( _
From line In IO.File.ReadAllLines("States.txt") _
Where line.Length > 0 _
Let Items = line.Split(","c) _
Select SingleState = Items(0) & " - " & Items(1)).ToArray
Sample content
Code:
ALABAMA,AL
ALASKA,AK
AMERICAN SAMOA,AS
ARIZONA,AZ
ARKANSAS,AR
CALIFORNIA,CA
COLORADO,CO
CONNECTICUT,CT
DELAWARE,DE
DISTRICT OF COLUMBIA,DC
FEDERATED STATES OF MICRONESIA,FM
A somewhat more detailed example which shows how you might display the data in a DataGridView. Note Select New is used rather than Select.
Code:
Dim States = (From line In IO.File.ReadAllLines("States.txt") _
Let Items = line.Split(","c) _
Select New StateCodes(Items(0), Items(1))).ToDataTable
bsStates.DataSource = States
DataGridView1.DataSource = bsStates
Extension for the above
Code:
Module Extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function ToDataTable(Of T) _
(ByVal value As IEnumerable(Of T)) As DataTable
Dim returnTable As New DataTable
Dim firstRecord = value.First
For Each pi In firstRecord.GetType.GetProperties
returnTable.Columns.Add(pi.Name, _
pi.GetValue(firstRecord, Nothing).GetType)
Next
For Each result In value
Dim nr = returnTable.NewRow
For Each pi In result.GetType.GetProperties
nr(pi.Name) = pi.GetValue(result, Nothing)
Next
returnTable.Rows.Add(nr)
Next
Return returnTable
End Function
End Module
Re: Read CSV Data into textbox
Thanks guys, i have it reading a text file.
I have a listbox, and if someone clicks, say, Bob on the listbox, it reads a line from the file, and if myCSVArray(0) is not Bob, then read the next line.. untill it is Bob, then display the data, split at each comma.
But when it reaches the end of the file, and i search for something at the top of file, it carries on trying to read the next line. Which, of course, is nothing, because it has reached EOF.
I Have:
Code:
If sr.EndOfStream = True Then
End If
How would i get it to start at the top of the file if sr.EndOfStream = true ?
- Thnks
Re: Read CSV Data into textbox
Quote:
Originally Posted by
Lukeshepp
How would i get it to start at the top of the file if sr.EndOfStream = true ?
Generally speaking, you wouldn't. Using a StreamReader, you would normally read from the beginning to end of the file and that's that.
One option you do have is to set the Position of the StreamReader's BaseStream back to zero, which puts you back at the beginning of the file again. I would recommend against that though.
If you want to access the data more then once then don't read it line by line. Call File.ReadAllLines to read all the data into a String array. You can then loop over that array as many times as you like, or access the data in a completely random manner.
Re: Read CSV Data into textbox
Quote:
Originally Posted by
Lukeshepp
Hello,
I want to be able to load & read (and later edit) .csv files into a textbox in Visual Basic 2008, If possible.
I don't know anything about this at all, but i do know a quite a bit about visual basic..
You would normally post the code you already have around about now, but i don't have any.. yet :(
Any help/code would be really appreciate..
Thanks!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
TextBox2.Text = OpenFileDialog1.FileName.ToString()
TextBox1.Text = System.IO.File.ReadAllText(TextBox2.Text)
You open your csv file and it will display in textbox 1 , remember to activate multiline on textbox 1