[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

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
0
Fordraiders
Asked:
Fordraiders
  • 2
1 Solution
 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now