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
SAVE and LOAD DataGridView with TEXT file
Page 1 of 12 Replies - 1486 Views - Last Post: 15 January 2013 - 01:28 AM
Replies To: SAVE and LOAD DataGridView with TEXT file
#2
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:
But maybe that's just me. Cheers!
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:
4runner, on 14 January 2013 - 06:29 AM, said:
Handles butLoad.Click
But maybe that's just me. Cheers!
#3
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|