|
-
Mar 26th, 2008, 09:10 AM
#1
Thread Starter
Addicted Member
[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
-
Mar 26th, 2008, 09:17 AM
#2
Re: [2005] Save datagridview content in user setting
Is your DGV bound to a data source?
-
Mar 26th, 2008, 10:36 AM
#3
Thread Starter
Addicted Member
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 ?
-
Mar 26th, 2008, 12:47 PM
#4
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?
-
Mar 27th, 2008, 03:04 PM
#5
Thread Starter
Addicted Member
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...
-
Mar 27th, 2008, 03:49 PM
#6
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
-
Mar 31st, 2008, 01:26 AM
#7
Thread Starter
Addicted Member
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 ?
-
Mar 31st, 2008, 01:31 AM
#8
Thread Starter
Addicted Member
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
-
Mar 31st, 2008, 07:48 AM
#9
Re: [2005] Save datagridview content in user setting
 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.
-
Apr 13th, 2008, 11:11 PM
#10
Thread Starter
Addicted Member
Re: [2005] Save datagridview content in user setting
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|