2 Replies - 6783 Views - Last Post: 15 January 2013 - 01:28 AM Rate Topic: -----

#1 4runner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 13

SAVE and LOAD DataGridView with TEXT file

Posted 14 January 2013 - 06:29 AM

I love simple solutions, thought I would share this one. Took me all day to nail it.
Here is a simple way to Save and Load data into a DataGridView using plain text file.

1. Start a new Form.
2. Add a DataGridView and 2 buttons "butSave" and "butLoad".
3. Add the code below and run.


Public Class Form1

Dim ThisFilename As String = Application.StartupPath & "\MyData.dat"

Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
SaveGridData(DataGridView1, ThisFilename)
End Sub

Private Sub butLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLoad.Click
LoadGridData(DataGridView1, ThisFilename)
End Sub

Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
ThisGrid.SelectAll()
IO.File.WriteAllText(Filename, ThisGrid.GetClipboardContent().GetText.TrimEnd)
ThisGrid.ClearSelection()
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

End Class

Is This A Good Question/Topic? 0
  • +

Replies To: SAVE and LOAD DataGridView with TEXT file

#2 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 166
  • Joined: 18-October 12

Re: SAVE and LOAD DataGridView with TEXT file

Posted 14 January 2013 - 03:00 PM

There are many versions of "Command Button" in Hungarian notation. Personally, I use btnSave or btnLoad, but I've also seen "cmdSave" and "cmdLoad," as well as "cbSave" and "cbLoad."

It is the opinion of this poster that you've designed your personal Hungarian abbreviation entirely around those odd moments where you get to type the phrase:

View Post4runner, on 14 January 2013 - 06:29 AM, said:

Handles butLoad.Click



But maybe that's just me. Cheers!
Was This Post Helpful? 0
  • +
  • -

#3 4runner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 13

Re: SAVE and LOAD DataGridView with TEXT file

Posted 15 January 2013 - 01:28 AM

Hi C.Andrews
Thanks for that, I've never thought about it. I use it ALL the time in my code. Can't remember where I picked it up from. Thinking it might have been the default in VB DOS. The default in VB.NET is "Button" which I think is too long and so I always just change it to something more descriptive.

But I agree it would be better to go with a more widely use convention. I'm just wondering what other things I have "personalized" which aren't common practice.
Cheers
James
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1