Perf
Perf

Reputation: 23

Keep data in memory

I have this code, in which I read the data i deserialized in a gridview, let's name it FormReadDatabases It gets populated like this:

xmlData = (xml.ServiceConfig)serializer.Deserialize(reader);
dataGridView1.DataSource = xmlData.Databases;

Then in each row of the grid I have a button 'Tables'
After I click it a new form appears FormReadTables

It gets populated like this:

BindingList<xml.Table> table = new BindingList<xml.Table>();
dataGridView4.DataSource = table;

Then I have a button which helps me add a new table, it works fine, the new row appears in the FormReadTables, But when i close the form and I am now at the FormReadDatabases if I click again on the Table button the changes are not saved.

Any idea how to avoid this?

Upvotes: 0

Views: 1394

Answers (2)

atom.gregg
atom.gregg

Reputation: 1007

Each time the form opens, you are creating a new BindingList.

BindingList<xml.Table> table = new BindingList<xml.Table>();

Instead, have the other page contain a variable for this, and when you 'new' the other form, pass in the variable.

The Actions taken on the opened form are byref, and therefore will update your host forms variable. This means the next time you open the form, the variable you pass to it will already have the previous changes already stored in it.

Example as requested:

I don't have my WinForms environment at hand, but this shows the important concepts.

namespace Bob
{
    public class FormLucy
    {
        private BindingList<xml.Table> table = new BindingList<xml.Table>();

        // your form stuff.. 

        protected void ButtonClick(object sender, EventArgs e)
        {
            var frm = new FormTracy(table);

            // init your forms properties, position etc

            fmr.ShowDialog();
        }
    }
}

Upvotes: 1

Mrinal Kamboj
Mrinal Kamboj

Reputation: 11478

This should be simple, data binding needs to happen using a mechanism that can hold value even when forms are opened or closed:

First way could be use a static type as follows:

static BindingList<xml.Table> table;

public BindingList<xml.Table> FetchTable()
{
if(table == null)
{
 table = new BindingList<xml.Table>();
}
return table
}

dataGridView4.DataSource = FetchTable();

There's a catch out here what if the form can have multiple instances than can access the static variable, then while updating the table type it needs to be locked / synchronized

Another option would be table type is part of the main form, which loads the child form and in the constructor of the child form it gets the instance of the parent form, which is used updated and is retained even after closing child form. This will also need synchronization for multiple user / thread access

public class ParentForm
{
public BindingList<xml.Table> table = new BindingList<xml.Table>();
}

public class ChildForm
{
ParentForm localPf;
pulic ChildForm(ParentForm pf)
{
localPf = pf;
}
dataGridView4.DataSource = localPf.table;
}

Noe any change to parent form object's table variable will persist till the point Parent form is in the memory, but please note this implementation is not yet threads safe

Upvotes: 2

Related Questions