Reading txt-files into datagridview

JStaseins

New member
Joined
Jul 8, 2008
Messages
4
Programming Experience
3-5
Hello
The reading of a txt-file into a datagridview doesn't work quite well. The creating of the txt-file was no problem, only when I try to read the txt-file, VB.net gives the error "Path/File access error". This is the code:

VB.NET:
   Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
        With SaveFileDialog1
            .Filter = "Woordenlijst (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 0
            .InitialDirectory = "c:\Mijn documenten"
            .Title = "Gegevens opslaan"
            .ShowDialog()
        End With

        Dim intK As Integer

        Dim bestand As String = SaveFileDialog1.FileName
        intK = FreeFile()
        FileOpen(intK, SaveFileDialog1.FileName, OpenMode.Output)
        For i As Integer = 0 To 16
            For j As Integer = 0 To 1
                Print(intK, TblwoordenDataGridView.Rows(i).Cells(j).Value.ToString() & vbNewLine)
            Next
        Next
        FileClose()

    End Sub
    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open.Click


        With OpenFileDialog1
            .Filter = "Woordenlijst (*.txt|*.txt|All files (*.*)|*.*"
            .FilterIndex = 0
            .InitialDirectory = "c:\Mijn documenten"
            .Title = "Gegevens laden"
            .ShowDialog()
        End With

        Dim intK As Integer

        intK = FreeFile()
        FileOpen(intK, SaveFileDialog1.FileName, OpenMode.Input)
        For i As Integer = 0 To 16
            For j As Integer = 0 To 1
                Print(intK, TblwoordenDataGridView.Rows(i).Cells(j).Value.ToString())
            Next
        Next
        FileClose()

    End Sub

Does anyone know how to solve this problem?
Thanks
 
Thanks for quick help. I managed to read the txt-file with My.Computer.FileSystem.ReadAllText and to copy it in the msgbox, but I have no idea how to put it in the right order back into the datagridview. This is the code:

VB.NET:
    Private Sub Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open.Click


        With OpenFileDialog1
            .Filter = "Woordenlijst (*.txt|*.txt|All files (*.*)|*.*"
            .FilterIndex = 0
            .InitialDirectory = "c:\Mijn documenten"
            .Title = "Gegevens laden"
            .ShowDialog()
        End With



        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim reader As String
            reader = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName, System.Text.Encoding.ASCII)
            MsgBox(reader)
        End If
end sub
 
The DataGridView is for displaying data. You would be much better off adding your data to a dataset or datatable and then binding that to your datagridview control for viewing.

By the way FileSystem.ReadAllText returns a single string of all the text and not just the line needed to add to a row.
 
You need to tell us what the text data looks like. For display in a datagridview it would be best if it was delimited or fixed width and tabular structure, like CSV, TSV, etc..
 
I'm not that experienced with anything that has to do with the use of databases in vb.net. But I was abled to solve the problem using streamreader. Thanks for all reactions.
 

Latest posts

Back
Top