Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Is this possible to get all the cell every row of a datagrid then pass it to a List

like columns of:

FirstName LastName Age Gender
Ron Miler 23 Male
Steve Palioc 20 Male
Sarah Carter 18 Female


I want to save the List to my.setting so that when ever I close my winForm it will save the latest item of the datagrid, and when I start Open my winForm it will just load the data
datagrid may only have 40 rows..

Thanks in Advance
Posted
Comments
Kschuler 11-Jun-13 15:53pm    
You want to save one row? Or the entire grid? If you have a lot of data, I suggest that you use a different method of saving the data. Like saving to a text file or a database. If you absolutely MUST use a setting, why not just turn the grid into a delimited string of some sort and save it as a string? (A pipe delimited would be good as it's not likely that you're storing data that uses a pipe character.)
iMaker.ph 12-Jun-13 21:41pm    
I've been thinking about using a pipe but am just curious if is it possible to save a grid on a setting aside from string collection.

1 solution

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:
VB
Public Class Form1

    Private clsDS As New DataSet 'Class level variable

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'When loading the form, check to see if the setting is there.
        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
        'The first time the app is run, it doesn't have any schema to allow users to add rows in the grid
        'This method is just so my test app will work.
        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.
 
Share this answer
 
Comments
iMaker.ph 18-Jun-13 5:10am    
This work fine thanks but how about if I wan't to add another row and save it to gblSettingDataSet
Kschuler 18-Jun-13 8:45am    
That's what the btnSaveGridToSettings is for...whatever is in the grid which is databound to gblSettingDataSet should be saved. Add another row to your grid with data, then click the save to grid button, then close the program. When you open it again it should have the row you added.
iMaker.ph 18-Jun-13 10:52am    
dt.Rows.Add(dr)
ds.Tables.Add(dt)

I got this exception when adding rows in my datagrid

DataTable already belongs to this DataSet.

BTW Thanks for the reply :)
Kschuler 18-Jun-13 10:59am    
You only need to add a DataTable to a DataSet once. If the table is already there, you can access it like this:
ds.Tables(0).Rows.Add(dr)

If you have more than one DataTable in your DataSet, you will probably want to use a string table name instead of an index. (In my example, I used an index of zero to access the first, and I assume, only table in the set)
iMaker.ph 18-Jun-13 11:06am    
Thank it works perfectly :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900