Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Save datagridview content in user setting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Resolved [RESOLVED] [2005] Save datagridview content in user setting

    Hi,
    I have been having a hard time trying to figure out how to save the content of a datagridview to a user setting System.Collections.Specialized.StringCollection.

    My goal is to have a list of wors that can be updated by the user. So he/she can add or remove... and then I want it to be saved.

    I thought the best way was to have a datagridview, and then save it to a System.Collections.Specialized.StringCollection but I am a bit confused as to how to do it exactly.

    I would greatly appreaciate help,
    thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Save datagridview content in user setting

    Is your DGV bound to a data source?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Re: [2005] Save datagridview content in user setting

    No it is not.
    Each user would have their own words.
    Is not the my.settings the best way ?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Save datagridview content in user setting

    So your datagridview will have only 1 single column to display the list of words, and you want to save/load that list of words to/from My.Settings which is a stringCollection object, right?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Re: [2005] Save datagridview content in user setting

    yeap that's exactly it....
    if there is a better way that my.setting i don't mind either...
    i thought it was the most logical...

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Save datagridview content in user setting

    Okie... To load the data, you'd convert the string collection to a datatable, then bind it to your datagridview. To save the data, you convert the datatable back to a string collection, then save it to my.settings. Some thing like this
    Code:
    'Suppose Testing is the name of the stringcollection in My.Settings
        'that you want to load to your DataGridView
    
        Private Sub LoadMySettingData()
            Dim dt As DataTable = Me.ConvertStringCollectionToDataTable()
            Me.DataGridView1.DataSource = dt
        End Sub
    
        Private Function ConvertStringCollectionToDataTable() As DataTable
            Dim dt As New DataTable()
            dt.Columns.Add("MyItems", GetType(String))
            For Each item As String In My.Settings.Testing
                dt.Rows.Add(New String() {item})
            Next
            Return dt
        End Function
    
        Private Function ConvertDataTableToStringCollection(ByVal table As DataTable) As System.Collections.Specialized.StringCollection
            Dim strColl As New System.Collections.Specialized.StringCollection()
            For Each row As DataRow In table.Rows
                strColl.Add(row(0).ToString)
            Next
            Return strColl
        End Function
    
        Private Sub SaveSettings()
            Dim dt As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
            My.Settings.Testing = Me.ConvertDataTableToStringCollection(dt)
            My.Settings.Save()
        End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Re: [2005] Save datagridview content in user setting

    hi Stanav, thanks,
    I tried to implement your way, but it says type "datatable" not defined when i declare dt as datatable ?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Re: [2005] Save datagridview content in user setting

    I did instead:
    Dim dt As Data.DataTable = New Data.DataTable("ParentTable")

    does it look right ?

    thanks

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Save datagridview content in user setting

    Quote Originally Posted by Zoroxeus
    I did instead:
    Dim dt As Data.DataTable = New Data.DataTable("ParentTable")

    does it look right ?

    thanks
    Yes, if you don't import System.Data namespace to your class, you have to use fully qualified name just as you have discovered.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    174

    Resolved Re: [2005] Save datagridview content in user setting

    Excellent thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width