Resolved DataGridView Settings

amnon

Member
Joined
Dec 18, 2022
Messages
16
Programming Experience
Beginner
Hello,
So far, I have stored the DataGridView settings like DataGridViewAutoSizeColumnsMode in the application settings (My.Settings), but I would like to be able to change and save them depending on the user. Could you give me some tips, I was thinking of creating xml file and storing these settings threre, and loading them at startup.

Best Regards
amnon
 
  • Scope indicates if the property is read-only. If the value is Application, the property is read-only; if the value is User, the property is read-write.
 
I created a sample program with two forms. On the first form I have DataGridView with button "DataGridView settings", which loads form2. On Form 2 I have check boxes responsible for DataGridView settings and one button "save setiings" which save all settings. Maybe it will be useful to someone.

Form 1 - Load
VB.NET:
Expand Collapse Copy
        If My.Settings.MyAllowUserToResizeColumns = True Then
            DataGridView1.AllowUserToResizeColumns = True
        Else
            DataGridView1.AllowUserToResizeColumns = False
        End If

        If My.Settings.MyAllowUserToResizeRows = True Then
            DataGridView1.AllowUserToResizeRows = True
        Else
            DataGridView1.AllowUserToResizeRows = False
        End If

        If My.Settings.MyAutoSizeColumnsMode = 16 Then
            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        Else
            DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
        End If



Form 2 - save button
VB.NET:
Expand Collapse Copy
Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click

        If chk_AllowUserToResizeColumns.Checked = True Then
            Form1.DataGridView1.AllowUserToResizeColumns = True
            My.Settings.MyAllowUserToResizeColumns = True
        Else
            Form1.DataGridView1.AllowUserToResizeColumns = False
            My.Settings.MyAllowUserToResizeColumns = False
        End If

        If chk_AllowUserToResizeRows.Checked = True Then
            Form1.DataGridView1.AllowUserToResizeRows = True
            My.Settings.MyAllowUserToResizeRows = True
        Else
            Form1.DataGridView1.AllowUserToResizeRows = False
            My.Settings.MyAllowUserToResizeRows = False
        End If

        If chk_AutoSizeColumnsMode.Checked = True Then
            Form1.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            My.Settings.MyAutoSizeColumnsMode = 16
        Else
            Form1.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            My.Settings.MyAutoSizeColumnsMode = 1
        End If

        Try
            My.Settings.Save()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Close()

End Sub
 
You can simplify this:
If My.Settings.MyAllowUserToResizeColumns = True Then
DataGridView1.AllowUserToResizeColumns = True
Else
DataGridView1.AllowUserToResizeColumns = False
End If
to this:
VB.NET:
Expand Collapse Copy
DataGridView1.AllowUserToResizeColumns = My.Settings.MyAllowUserToResizeColumns
 
My.Settings.MyAutoSizeColumnsMode = 16
It is better if you change the setting type to DataGridViewAutoSizeColumnsMode. In type selection click 'Browse...' and find it under System.Windows.Forms.
 
Thank you JohnH for your tips, very useful.
I would like to ask, how can I use this code or a part of it as a template for my next projects?

Regards
Amnon
 
Back
Top