Hi, I have a simple program with datagrid view bounded to MsAccess DB in Visual Basic 2010.
When user changes the width of datagridview columns and restarts the program, width of the columns have been reseted to original state. Now, what code can I use and how can I save changes?

Thank you.

Google: persist datagrid settings

I'm using VB 2010 and code I'm using isnt working. I'm trying Please help:

On form load event I use this code: (I have datagridview with 8 columns)

Try
            Dim location As Point = My.MySettings.Default.Dvflocation
            Dim size As Size = My.MySettings.Default.Dvfsize
            Me.StartPosition = FormStartPosition.Manual
            Me.Location = location
            Me.Size = size
            Dim cols As StringCollection = My.MySettings.Default.Dvfcolumns

            Dim colsArray As String() = New String(cols.Count - 1) {}
            cols.CopyTo(colsArray, 0)
            Array.Sort(colsArray)
            For i As Integer = 0 To colsArray.Length - 1
                Dim a As String() = colsArray(i).Split(","c)
                Dim index As Integer = Integer.Parse(a(3))
                Me.TelefoniDataGridView.Columns(index).DisplayIndex = Int16.Parse(a(0))
                Me.TelefoniDataGridView.Columns(index).Width = Int16.Parse(a(1))
                Me.TelefoniDataGridView.Columns(index).Visible = Boolean.Parse(a(2))
            Next


        Catch ex As NullReferenceException

        End Try

Now, for the form closing event I use this code:

Dim stringCollection As New StringCollection
        Dim i As Integer = 0
        For Each column As DataGridViewColumn In Me.TelefoniDataGridView.Columns
            stringCollection.Add(String.Format("{0},{1},{2},{3}", column.DisplayIndex.ToString("D2"), column.Width, column.Visible, System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)))
        Next
        My.MySettings.Default.Dvfcolumns = stringCollection
        'Properties.Settings.[Default].DataGridViewFormColumns = stringCollection
        If Me.WindowState = FormWindowState.Normal Then
            My.MySettings.Default.Dvflocation = Me.Location
            My.MySettings.Default.Dvfsize = Me.Size
        Else
            My.MySettings.Default.Dvflocation = Me.RestoreBounds.Location
            My.MySettings.Default.Dvfsize = Me.RestoreBounds.Size


        End If
        My.MySettings.Default.Save()

dvfsize, dvflocation and dvfcolumns are set in settings file.
also, i'm using "imports System.Collections.Specialized" reference

"TelfoniDataGridView" is name of datagridview.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.