Okay...so
MSDN[
^] says "Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string."
I tried to use a DataGridView as a setting, but it didn't work. It must not be serializeable. However, I knew that a DataSet is serializeable. So I setup my grid so that it would be data bound to a DataSet object and saved/loaded that into a setting. It worked.
First I made a setting in the properties window called
gblSettingDataSet
. DataSet isn't a data type that is provided, but I clicked browse and found System.Data.DataSet and selected it. Then I added a DataGridView and two buttons. Here is the code:
Public Class Form1
Private clsDS As New DataSet
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If My.Settings.gblSettingDataSet IsNot Nothing Then
clsDS = My.Settings.gblSettingDataSet
End If
If clsDS.Tables.Count > 0 Then
DataGridView1.DataSource = clsDS.Tables(0)
End If
End Sub
Private Sub btnInitializeGridFirstTime_Click(sender As System.Object, e As System.EventArgs) Handles btnInitializeGridFirstTime.Click
If clsDS.Tables.Count = 0 Then
Dim dt As New DataTable
dt.Columns.Add("A")
dt.Columns.Add("B")
dt.Columns.Add("C")
clsDS.Tables.Add(dt)
End If
DataGridView1.DataSource = clsDS.Tables(0)
End Sub
Private Sub btnSaveGridToSettings_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveGridToSettings.Click
My.Settings.gblSettingDataSet = clsDS
My.Settings.Save()
End Sub
End Class
This works, but again...I don't recommend this if your grid could end up with a lot of data or if you are building a program that needs to be professional. Storing large amounts of data is what databases are for.