SAVE AND RESTORE LAYOUT OF DATAGRIDVIEW

Hi All,

I want let user to layout the default datagridview layout.
I want to save the layout change by user and to restore every time the user visit it.

Means every user have their own layout.

How could I do it ? I know there are several ways to save it, to (text, xml and etc) file, application setting, database and etc.

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?
 
CodeCruiserCommented:
You can create xml files or plain text files. Better still, add settings to DataTable and then use WriteXML so you can readXML later to get the datatable back.
0
 
emi_sastraAuthor Commented:
I know there are several ways to save it, to (text, xml and etc) file, application setting, database and etc.

Which one is the best ? But I don't know yet how to save and restore it.

Thank you.
0
 
emi_sastraAuthor Commented:
I try below code and after I quit the application I don't see data at the setting ?

  Private Sub Form_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If My.Settings.ColumnOrders1 Is Nothing Then
            My.Settings.ColumnOrders1 = New System.Collections.Specialized.StringCollection
            My.Settings.ColumnWidth1 = New System.Collections.Specialized.StringCollection
        End If

        My.Settings.ColumnOrders1.Clear()
        My.Settings.ColumnWidth1.Clear()

        For Each dc As DataGridViewColumn In dgvData.Columns
            My.Settings.ColumnOrders1.Add(dc.DisplayIndex.ToString())
            My.Settings.ColumnWidth1.Add(dc.Width.ToString())
        Next

        My.Settings.Save()
    End Sub

What's the problem ?

Thank you.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CodeCruiserCommented:
Did you try using Closing event instead of Closed event? Is code executing fine if you step through it?
0
 
emi_sastraAuthor Commented:
- Did you try using Closing event instead of Closed event?
No, I will try it now.

- Is code executing fine if you step through it?
No exception.

Thank you.
0
 
emi_sastraAuthor Commented:
I provide exception, but there is no exception.

Thank you.
0
 
CodeCruiserCommented:
So closing event did not help? Does it save if you use some plain string setting?
0
 
emi_sastraAuthor Commented:
- So closing event did not help?
No.

- Does it save if you use some plain string setting?
What makes it different ?

Thank you.
0
 
emi_sastraAuthor Commented:
I have no idea how to code it if change to string from collection to string type.

Thank you.
0
 
emi_sastraAuthor Commented:
I try below and still get nothing at project setting.

   Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Try

            If My.Settings.ColumnOrders1 Is Nothing Then
                My.Settings.ColumnOrders1 = ""
                My.Settings.ColumnWidth1 = ""
            End If

            My.Settings.ColumnOrders1 = ""
            My.Settings.ColumnWidth1 = ""

            For Each dc As DataGridViewColumn In dgvData.Columns
                My.Settings.ColumnOrders1 = dc.DisplayIndex.ToString()
                My.Settings.ColumnWidth1 = dc.Width.ToString()
                Exit For
            Next

            My.Settings.Save()

        Catch ex As Exception

        End Try
       
    End Sub

Thank you.
0
 
CodeCruiserCommented:
Are you looking at the right file? Debug vs Release folder.
0
 
emi_sastraAuthor Commented:
- Are you looking at the right file? Debug vs Release folder.
I see it at project properties at setting menu, am I wrong ?

Thank you.
0
 
CodeCruiserCommented:
Try checking the projectname.exe.config file
0
 
emi_sastraAuthor Commented:
Below are the setting :

<userSettings>
        <ERVSoftware.My.MySettings>
            <setting name="ColumnOrders1" serializeAs="String">
                <value />
            </setting>
            <setting name="ColumnWidth1" serializeAs="String">
                <value />
            </setting>
        </ERVSoftware.My.MySettings>
    </userSettings>

No value there ?

Thank you.
0
 
CodeCruiserCommented:
My bad. User settings are stored in user profile folder. At application load, try reading that setting and use msgbox to see its value.
0
 
emi_sastraAuthor Commented:
- User settings are stored in user profile folder.
Where is it ?

- At application load, try reading that setting and use msgbox to see its value.
How to read it ?

Thank you.
0
 
CodeCruiserCommented:
Use

msgbox(My.Settings.settingname)
0
 
emi_sastraAuthor Commented:
Return 0

Would you please provide sample that works ?

Thank you.
0
 
CodeCruiserCommented:
All my samples work. I just add a user level setting in my project properties and then I can just modify these at runtime without any problem. Does it work on a button click?
0
 
emi_sastraAuthor Commented:
I am sorry just back.

Weird, still can not solve the problem.

I will ask for several questions regarding this topics.

1. What is the best media to save (xml, config file) to save layout when its size could become huge, since it allow user to change it independenttly, every user has their own style of grid. Should separate file for each form or grid.

2. For the purpose of point 1, what should be saved to file.

Hope you can help for this.

Thank you.
0
 
CodeCruiserCommented:
Config files are xml files so its same thing. Each user has its own file. If its just the width and order of columns then you can just store a string like

columnname:width:order|columnname:width:order|...
0
 
emi_sastraAuthor Commented:
- Config files are xml files so its same thing.
Could we create more than one config files ?
Using xml we can create as many as we wish based on the form name.

-  If its just the width and order of columns then you can just store a string like
Since user could change the style : header columns height, columns width, columns shown, columns order, datetime format, number format.

Thank you.
0
 
emi_sastraAuthor Commented:
Ok.

Thank you very much for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.