Reordering columns of datagridview not working
<p>I save the displayed index of each column of my Datagrid before terminating to a config file.
When restarting the application, I want to restore the order the columns where before terminating.
This does not work.
Here is my code for reordering:</p>

<pre><code>   Private Sub reorderColumns()
        'convert the list of KeyValuePairs to a dictionary for easier access
       Try
            For Each column As DataGridViewColumn In MainDGV.Columns
                column.DisplayIndex = SettingsKeeper.getInstance.positionOfColumnInGrid(column.Name)
            Next
        Catch ex As KeyNotFoundException
            'Ignore, this means the config was not good, which generally means it was not yet
            'created once
        Catch ex As Exception
            printErrorMessage(ex,ErrorCodes.Class_ReorderColumns)
        End Try
    End Sub
</code></pre>

<p>in my initialization function, I followed the advise from this <a href="https://stackoverflow.com/questions/629521/datagridview-column-order-does-not-seem-to-work">post</a> regarding the AutogenerateColumns attribute, but that didn't solve anything. </p>

<p>this is my defacto constructor, which needs to be called after initialization:</p>

<pre><code>Public Sub init()
        dt = New DataTable
        Dim arr(CentralControl.dgvColumns.Count - 1) As String

        Dim i As Integer = 0
        For Each att As String In CentralControl.DataTemplate.Keys
            dt.Columns.Add(att, GetType(String))
            arr(i) = "No Data"
            i += 1
        Next

        For i = 1 To SettingsKeeper.getInstance.numberRows
            dt.Rows.Add(arr)
        Next

        MainDGV.AutoGenerateColumns = True
        MainDGV.DataSource = dt
        MainDGV.AutoGenerateColumns = False
        reorderColumns()
    End Sub
</code></pre>

<p>my DataTemplate is in reality a dictionary that contains the name of datacolumn as key and some rules for calculating the value for this column as value of the dictionaries entry.</p>