views:

695

answers:

3

I have some data Grid Views and I want the user to be able to keep changes he does to them. Not the Data changes, only the layout changes, like the width, the hight, the order of the columns and maybe the visibility of them. I don't care if it will be automatic or by clicking a button...

Edit: I found a way to do this using Settings.I have added the settings file, but I have no idea of how I'm supposed to add column order or column size in those settings

A: 

I am assuming you mean the end user, if so then I would store that info in cookie/database and pull it on load to customize their experience.

Dustin Laine
That's what I'm thinking of doing, but I don't know how...
Ant
A: 

In each of your event handlers for resizing/sorting you could have a helper function that writes the layout instructions to a storage medium like a db, registry, xml, config, etc... and read those instructions to draw the interface when the application loads.

JMP
All of the controls I have for the Data Grid Views are auto generated by the Designer, so the ability to, let's say, reordering is a Function and I can't (or at least don't know how to) add something in MS's pre-builded Functions and/or Classes
Ant
I dig, in that case you may need to explicitly manage those events if you want to be able to implement the sort of advanced functionality that you're looking for. That is -- there isn't a SaveLayout() type of function you could call that would save layout/sorting information for you. Sorry :(
JMP
Like I said I don't care if it is going to be automatic or with the click of a button
Ant
A: 

The kind of formatting information you want to save can be found in the various properties of the DataGridGiew. You may have to spend some quality time with its object model documentation, but you should be able to find most of what you're looking for.

For example, to save the way the rows have been sorted, you can persist these two properties:

DataGridView.SortedColumn tells you on which column's values the rows were sorted. DataGridView.SortOrder tells you the order (i.e. ascending or descending)

And you should be able to find info about column widths and order on the members of the DataGridView.Columns collection.

I think it's a matter of preference whether you record these values all at once (e.g. click a "save settings" button) or one by one in event handlers as the properties get changed. I like to save them automatically when the form is closed w/out any action by the user. It's been a while since I implemented anything like this, but I recall the app settings being a pretty handy place to keep this sort of info. In the Project Properties Settings designer, just define settings for the stuff you want to keep (make sure to set the Scope to "user"). Then assign and load actual values in your code at run-time, like so:

// e.g. you defined an integer user setting at design time called ColA_Width, to
// hold the value of the width of "ColA" in your DataGridView.

// Use this code to save the value (either in a specific event handler, or a 
// global "save settings" routine).
Properties.Settings.Default.ColA_Width = MyDataGridViewInstance.Columns["ColA"].Width;
Properties.SEttings.Default.Save();

// Then you'd reverse the assignments when the app next loads so that the saved 
// settings are "remembered" between user sessions.
MyDataGridViewInstance.Columns["ColA"].Width = Properties.Settings.Default.ColA_Width;

The only caveats I can recall are that some of the DataGridView property values are not straightforward to save as strings (remember that your user settings are ultimately saved as XML), but it really depends on what sort of settings you're saving.

Matt