7 Replies - 11481 Views - Last Post: 08 June 2014 - 04:55 PM 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

This post has been edited by smohd: 08 June 2014 - 04:10 PM
Reason for edit:: code tags added


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: 169
  • 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
  • +
  • -

#4 javmiller  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-May 11

Re: SAVE and LOAD DataGridView with TEXT file

Posted 08 June 2014 - 04:02 PM

Hello, this is exactly the code i was looking for. It does what i need, except i get an error: InvalidCastException (conversion from string "" to type 'Double' is not vaild.)

In my datagridviews i am using integers or doubles as values, so it is not compatible with strings. Do you know a way to fix this?

Just in case you may need more clarification i am using this code to replicate some values across my gridview
Dim sum As Double = 0
            For i = 0 To MonthlyGrid.RowCount - 1
                sum += MonthlyGrid.Rows(i).Cells(4).Value()
            Next
            OverallGrid.Rows(0).Cells(3).Value = sum


Right here is where it brings up the error:
sum += MonthlyGrid.Rows(i).Cells(4).Value()

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: SAVE and LOAD DataGridView with TEXT file

Posted 08 June 2014 - 04:09 PM

Quote

In my datagridviews i am using integers or doubles as values, so it is not compatible with strings.
No that is not the problem, the problem is that in your values you have an empty row with nothing in it, so it can not cast an empty string to double. That was the message given below:

Quote

conversion from string "" to type 'Double' is not vaild

To fix this, if the empty row is always the one, then just ignore the last row: To MonthlyGrid.RowCount - 2. Else if you have some empty row in between, then you have to check if the return value is not "" string before adding to the sum.
Was This Post Helpful? 0
  • +
  • -

#6 javmiller  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-May 11

Re: SAVE and LOAD DataGridView with TEXT file

Posted 08 June 2014 - 04:25 PM

Ok, so i thought i was getting the hang of it. I fixed the first problem, but another arose.
I changed the part of the code to this:
  Dim sum As Double = 0
            For i = 0 To MonthlyGrid.RowCount - 2
                If Not MonthlyGrid.Rows(i).Cells(4).Value() = "" Then
                    sum += MonthlyGrid.Rows(i).Cells(4).Value()
                End If

            Next
            OverallGrid.Rows(0).Cells(3).Value = sum

Now a similar error arose when I save at the line of code:
 SaveGridData(MonthlyGrid, ThisFilename)

Was This Post Helpful? 0
  • +
  • -

#7 javmiller  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 05-May 11

Re: SAVE and LOAD DataGridView with TEXT file

Posted 08 June 2014 - 04:50 PM

Actually .... I found out that before i got my first reply i had changed
Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
to
Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal Filename As Double)
, which obvious wasn't going to work. I changed double back to string and now it works.

Thank you so much smohd! You were a great help. And thank you 4runner for the very useful and simple code!!
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: SAVE and LOAD DataGridView with TEXT file

Posted 08 June 2014 - 04:55 PM

you are welcomed, I am glad we could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1