anakic / Jot

Jot is a library for persisting and applying .NET application state.
http://www.codeproject.com/Articles/475498/Easier-NET-settings
MIT License
633 stars 56 forks source link

I want to persist DataGridView #25

Closed Nu-est closed 5 years ago

Nu-est commented 5 years ago

Hi all, i would like to persist all DataGridViewColumn state into json, but i have no idea how to do... I use Jot 1.4.1.0 version.

anakic commented 5 years ago

Here's how you can do it:

// I'm creating the tracker here, but you would usually have a single tracker 
// for the entire application so you might want to reference that instance instead
var tracker = new Jot.StateTracker();

// set up tracking for all columns
foreach (DataGridViewColumn col in songsDataGridView.Columns)
{
    tracker
        .Configure(col) // set up tracking for the column
        .IdentifyAs(col.Name) // identify column by name
        .AddProperties("Width") // track width (can add further columns)
        .RegisterPersistTrigger(nameof(FormClosed), songsDataGridView.FindForm()) // persist data when the this form is closed
        .Apply(); // apply previously stored state
}
Nu-est commented 5 years ago

Thank you anakic. It works very well. Did you know how to put the json together?

anakic commented 5 years ago

You're very welcome. Could you clarify your question about putting the JSON together?

Nu-est commented 5 years ago

I mean, i run the following code:

foreach (DataGridViewColumn col in songsDataGridView.Columns)
{
    tracker
        .Configure(col) // set up tracking for the column
        .IdentifyAs(col.Name) // identify column by name
        .AddProperties("Width") // track width (can add further columns)
        .RegisterPersistTrigger(nameof(FormClosed), songsDataGridView.FindForm()) // persist data when the this form is closed
        .Apply(); // apply previously stored state
}

will generate so many json. I wanna put all the json into a file such as songsDataGridView_Columns.json. Is there no way to solve this issue?

anakic commented 5 years ago

Ah ok, now I understand. Yes, by default, for each tracked object there will be a separate JSON file. This is to ensure that files are kept small so that reading and writing settings is fast. I don't think this is a big issue but I agree that it might be better to have just one file and use caching to ensure speed.

If you want to control exactly how and where data is stored, you can implement IStore and IStoreFactory interfaces and pass them into the StateTracker constructor. There's a short section about it in the readme.

I'll see if I can do something about this when I have a bit of time for Jot.

Nu-est commented 5 years ago

Ok anakic, I totally understand. So appreciated your time and effort. This case can be closed.