vb 2008 read/write to/from datagridview/text file

IBgemeda

New member
Joined
Dec 24, 2010
Messages
3
Programming Experience
Beginner
Hello. Code is for read and write to and from textfile and datagridview. vb 2008.
Works well enough.

Only problem is it doesn't save to text file the first input placed into any cell if it's the only input or the last of series of inputs.

Your time is much appreciated!
Thanks In advance.


VB.NET:
Imports System.IO

Public Class Form1

    'Reads file into form upon form load
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
    End Sub
    ' Arranges and Saves Data to a text file
    Private Sub ToolStripLabel2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel2.Click

        Dim fsStream As New FileStream("C:\Users\slutty\Desktop\Testing 1-2\datagridmessage.txt", FileMode.Create, FileAccess.Write)
        Dim swWriter As New StreamWriter(fsStream)

        Try
            For Each row As DataGridViewRow In Me.DataGridView1.Rows
                If Not row.IsNewRow Then
                    Dim values As New List(Of String)
                    For Each cell As DataGridViewCell In row.Cells
                        values.Add(CStr(cell.Value))
                    Next
                    swWriter.WriteLine(String.Join("=", values.ToArray))
                End If
            Next
            swWriter.Close()
        Catch
            swWriter.Flush()
            swWriter.Close()
            MsgBox("Saved")
        End Try
        swWriter.Close()
    End Sub
    ' Fill in the data grid with a List
    Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripLabel1.Click
        Dim filename As String
        filename = "C:\Users\slutty\Desktop\Testing 1-2\datagridmessage.txt"

        If File.Exists(filename) Then
            Dim iofile As New StreamReader(filename)
            Dim ioline As String
            Dim _name, _value

            Dim resultsDT As New DataTable
            resultsDT.Columns.Add("Name", GetType(System.String))
            resultsDT.Columns.Add("Value", GetType(System.String))

            Dim i As Integer

            Do While iofile.Peek() <> -1

                ioline = iofile.ReadLine
                Dim mysplit = Split(ioline, "=")
                For i = 0 To mysplit.Count - 1
                    _name = mysplit(0)
                    _value = mysplit(1)
                    i = i + 1
                    resultsDT.Rows.Add(_name, _value)
                    DataGridView1.DataSource = resultsDT
                Next
            Loop
            iofile.Close()
        Else
            MsgBox(filename + " doesn't exist!")
        End If

    End Sub
End Class
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,553
Location
Norway
Programming Experience
10+
What you describe probably mean the cell is still in edit mode, ie the user input has not been committed, to resolve call this method: DataGridView.EndEdit Method (System.Windows.Forms)

It is much easier to read/write a Xml text file with the DataTable ReadXml/WriteXml methods.
 
Top Bottom