datagridview column arrangements

vb.net 2008

Datagridview1

When my  datagridview is loaded with data. I need to make changes in column positions.
But the after I close the form and reopen. The datagrid view column arrangement is back in the old order..?

can the column arrangement be saved ?

Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul_Harris_FusionCommented:
Yes - it can but you will need to write a fair bit of code to make it happen.

You can either re-order the columns in code after every databind
OR
You can write code which writes away the column orderings (also whether they are hidden or not and their widths) into a settings file and loads them when the form is re-opened or re-bound

My company uses a proprietary file format for storing settings so my specific sample code will be of very litte use to you.  

However,  the following may spark some ideas:

            For Each column As DataGridViewColumn In MyGrid.Columns
                    dim info as string = column.Name & "~" & column.DisplayIndex & "~" & column.Visible.ToString
                    MySettingsFile.write info    ' pseudocode
            Next

When reading the settings you can do something like:
   for each info as string in MySettingsList    
       dim parts as string() = info.split ("~")
       dim colName as string = parts(0)
       dim colIndex as integer = Cint(parts(1))

       MyGrid.Columns.Item(colName).DisplayIndex = colIndex
   Next

Like I said, there is a fair bit of other code to write to plumb this all in and make it error resilient.
0
FordraidersAuthor Commented:
ok... I think I remember doing this in vb6...basically a text fiel thats keeps a record of changes and when loading and moving off the form it updates the writefile ?
0
Paul_Harris_FusionCommented:
That's correct.  

Under windows 7 guidelines you should store such information in a way that makes it specific to the user that generated it.   e.g. a file in a folder that is for the current user only.     However, these are guidelines only and there is no technical constraint.

The DataGridView control raises an event when the column order changes as follows:

Public Event ColumnDisplayIndexChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs)

which you could handle and use to write the new file providing the order is not being set by your code that reads the settings from the file!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.