none
trying to add a load button RRS feed

  • Question

  • I am making an event reminder and I have everything working except for the load button. The save button will save the data from the DataGridView to a text file and now what I need to do is somehow write a program that will read through that file and know where to put the data. I am super confused and most likely overthinking this whole process. This is the code I have:

    Public Class MainForm1 Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then Me.DataGridView1.EndEdit() Me.DataGridView1.Rows.RemoveAt(e.RowIndex) End If If DataGridView1.Columns(e.ColumnIndex).Name = "Column4" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then Dim Update As UpdateWindow Update = UpdateWindow Update.Show() End If End Sub Private Sub dltBtn_Click(sender As Object, e As EventArgs) Dim dltBtn As dltWindow dltBtn = dltWindow dltBtn.Show() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim Button1 As addBtn Button1 = addBtn Button1.Show() End Sub Private Sub UptBtn_Click(sender As Object, e As EventArgs) Dim UptBtn As UpdateWindow UptBtn = UpdateWindow UptBtn.Show() End Sub Dim thisFilename As String = Application.StartupPath & "\Event reminder.dat" Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click Me.Validate() Me.SaveGridData(DataGridView1, thisFilename) End Sub Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click Me.LoadGridData(DataGridView1, thisFilename) End Sub Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal thisFilename As String) 'ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText 'ThisGrid.SelectAll() 'IO.File.WriteAllText(thisFilename, ThisGrid.GetClipboardContent().GetText.TrimEnd) 'ThisGrid.ClearSelection() Dim FileToDelete As String FileToDelete = "saved.txt" If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete) End If Dim file As System.IO.StreamWriter file = My.Computer.FileSystem.OpenTextFileWriter("saved.txt", True) For Each row As DataGridViewRow In DataGridView1.Rows If Not row.IsNewRow Then file.WriteLine(row.Cells(0).Value.ToString & "~" & row.Cells(1).Value.ToString & "\n") End If Next file.WriteLine("EOF") file.Close() End Sub Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String) ThisGrid.Rows.Clear() For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine) ThisGrid.Rows.Add(Split(THisLine, " ")) Next End Sub Private Sub MainForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class

    So what I am thinking is somehow write a program in the LoadGridData that reads through the text file and knows that everything before the "~" needs to be loaded into cell 0 and everything after the "~" nedds to be loaded in cell 1. If I could get some help I would be much appreciative.

    Sunday, May 21, 2017 4:08 PM

All replies

  • The following is in one button click which were you would have the save and read in different events.

    Note the use of StringBuilder and File.WriteAllLines make things easier than the current method being used. For reading data back I use a TextFieldParser which was made for doing this type of work.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click -- save

    Dim fileName As String = "Your file name" Dim sb As New Text.StringBuilder For Each row As DataGridViewRow In DataGridView1.Rows If Not row.IsNewRow Then sb.AppendLine($"{row.Cells(0).ToString}~{row.Cells(1).ToString}") End If Next IO.File.WriteAllText(fileName, sb.ToString) -- read Using MyReader As New FileIO.TextFieldParser(fileName) MyReader.TextFieldType = FileIO.FieldType.Delimited MyReader.Delimiters = New String() {"~"} Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() DataGridView1.Rows.Add(New Object() {currentRow(0), currentRow(1)}) Catch ex As FileIO.MalformedLineException -- handle the exception e.g. write to log file etc. End Try End While End Using End Sub

    No need for EOF in the last line, I see it serving any real purpose. Lastly, add any assertions or conversations as you see fit for values read in.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, May 21, 2017 4:38 PM
    Moderator
  • R3D,

    You need *real* data behind it. The rest is then obvious.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, May 21, 2017 5:35 PM