question

MB-2323 avatar image
0 Votes"
MB-2323 asked MB-2323 edited

Keep DataGridView data after closing, without database

Hello,
please, is there any way how to save data that are in DataGridView when it's not connected to DB?

When I close the form and open it again, it should be still the same as before closing.

I'm a noob, I finally made a form with everything that I need but need this last thing.
This is my table:

 Public Class Form1

     Dim table As New DataTable("Table")
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
         table.Columns.Add("Date", Type.GetType("System.DateTime"))
         table.Columns.Add("Code", Type.GetType("System.String"))
         table.Columns.Add("Position", Type.GetType("System.String"))
         table.Columns.Add("Note", Type.GetType("System.String"))
         table.Columns.Add("Solved", Type.GetType("System.DateTime"))

         DataGridView1.DataSource = table
    
    
     End Sub

Can anyone post me any code to do that?
Thanks

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered MB-2323 edited

Initially try a simple approach like this:

 Dim table As New DataTable("Table")
 ReadOnly p As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "mydata.xml")
    
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
    If Not File.Exists(p) Then
       table.Columns.Add("Date", Type.GetType("System.DateTime"))
       table.Columns.Add("Code", Type.GetType("System.String"))
       table.Columns.Add("Position", Type.GetType("System.String"))
       table.Columns.Add("Note", Type.GetType("System.String"))
       table.Columns.Add("Solved", Type.GetType("System.DateTime"))
    Else
       table.ReadXml(p)
    End If
    
    DataGridView1.DataSource = table
    
 End Sub
    
 Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    
    table.WriteXml(p, XmlWriteMode.WriteSchema)
    
 End Sub
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Wow, thanks :-) .
One more question, this your code works, but I also have a button that marks solved row like this

     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

         DataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.PaleGreen

         DataGridView1.CurrentRow.Cells("Solved").Value = DateTimePicker1.Value

         DataGridView1.DataSource = table

         DataGridView1.ClearSelection()
    
     End Sub

When I reopen the form, the value is still there but not the green background. Any idea? :-)

0 Votes 0 ·

As an exception, try something like this:

 Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    
    If e.RowIndex < table.Rows.Count AndAlso DataGridView1.Columns("Solved").Index = e.ColumnIndex AndAlso Not Convert.IsDBNull(e.Value) Then
       DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.PaleGreen
    End If
    
 End Sub


1 Vote 1 ·

I should maybe mention one more thing :-D I'm sorry.
This code that you posted changing all Solved rows to green.
When I have the same button, to remove data from the current row's cell Solved and change it back to white, what to add? This button is for something like: "Oops, I "Solved" the wrong row".

Your code looks more complicated than mine so I'm lost changing it to white when it's empty again :-) . I was expecting that I'll be able to just edit code to white but this is too much for me.

0 Votes 0 ·