Link to home
Start Free TrialLog in
Avatar of ITSuperman
ITSupermanFlag for United States of America

asked on

Textfieldparser from CSV file - need to put into a datatable or db and show user

Someone showed me textfieldparser, which is working great, I have used it to pull in data from a csv file.  I can pull it in a line at a time, but I am looking for some way to pull in all the data into a table and show to the user.  Listview?  Datagridview?  I was using Gridview but it is not available for a windows form.  I would like to show this data to the user, let them edit, delete rows, or add, then I will have to send this data to a txt file.  Trying to find a good way to do this and possibly make it look decent.  Thanks for any help in advance!  I have been googling for hours trying to find good methods.
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

you can use a textfield parser to populate a datagridview.
show us some code that you have (and the source data file if possible)

Also, what do you mean that the datagridview is not available for a windows form?
Avatar of ITSuperman

ASKER

I was referring to the ASP.NET Gridview which gives you options to change the template(easy to make it look better) - I couldn't find anything equivalent to that in Windows Form.  

I have attached the code I was working with thanks to another person on here.  Just need to stream it into a datagridview if that is the best way to work with the data.  Thanks again for the help!
Option Strict On

Imports System.Text

Public Class Form1

    Private Sub btnProcessDelimitedFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcessDelimitedFile.Click
        ProcessDelimitedFile()
    End Sub

    Private Sub ProcessDelimitedFile()
        'Provide a test file
        Dim strFile As String = IO.Path.Combine(Application.StartupPath, "TestDelimited.txt")

        'If a valid file path to a .txt file has been selected....
        If Not IO.File.Exists(strFile) Then
            MessageBox.Show("File does not exist!", _
                            "File not found", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
            Return
        End If

        'Instantiate a reader with the file to process
        Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser = _
                My.Computer.FileSystem.OpenTextFieldParser(strFile)

        'Set the reader's TextFieldType to delimited 
        reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        'Set the readers Delimiters to a comma (,)
        reader.Delimiters = New String() {",", vbTab}
        'ignore the lines starting with this token
        reader.CommentTokens = New String() {"--"}

        ' Ready to read the file....
        Do While Not reader.EndOfData
            Try
                'Parse the line into fields using gthe ReadFields method
                Dim arrFields As String() = reader.ReadFields()

                'Process the data just read
                Dim strCurrentLine As String = String.Empty
                Dim t1 As Integer = 1
                Dim t2 As Integer = 2
                Dim tbox As String = "TextBox1"
                For Each strField As String In arrFields
                    strCurrentLine = strCurrentLine + strField + ";"
                    TextBox1.Text = strCurrentLine
                    TextBox2.Text = strField
                    t1 = t1 + 1
                    t2 = t2 + 1
                Next
                MessageBox.Show("The current line contains:" + _
                                Environment.NewLine + _
                                strCurrentLine, _
                                "Data read", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Information)

            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MessageBox.Show("Line " & ex.Message & _
                "is not valid and will be skipped.")
            End Try
        Loop

        'Close the reader
        reader.Close()
    End Sub

Open in new window

TestDelimited.txt
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America 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
That works great!  I am working on a button for the user to hit once they are done editing to send all of the datagridview to a text file with commas between fields now.
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
Looks great!  I will be testing this tonight and will give you full points on it.  
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
Thanks for the help!  Your solutions worked very well for what I was needing.
NP. Glad to help  : )