Question

I have this on framework v3.5 PRJ

public BindingList<BackupItem> bb = new BindingList<BackupItem>();

when

private void Form1_Load(object sender, EventArgs e)
{

    checkedListBox1.DataSource = bb;

    checkedListBox1.DisplayMember = "backupName";
}

I have implement add/edit/delete 'touching' only the BindingList all working as expected, at the end going to save/load the BindingList contents.

-Save done -Load done

hmm when loading the records not appear at checkedListBox1 !!

To display the records, I have to re init checkedListBox1 with

private void toolStripJSONLoad_Click(object sender, EventArgs e)
{
    string tmp = File.ReadAllText(@"D:\path.txt", Encoding.UTF8);

    JavaScriptSerializer oSerializer = new JavaScriptSerializer();
    bb = oSerializer.Deserialize<BindingList<BackupItem>>(tmp);

    checkedListBox1.DataSource = null;

    checkedListBox1.DataSource = bb;

    checkedListBox1.DisplayMember = "backupName";
}

anyone knows anything?

Was it helpful?

Solution

The statement:

bb = oSerializer.Deserialize<BindingList<BackupItem>>(tmp);

is creating a whole new object, pointing to a different memory location from where checkedListBox1.DataSource is pointing to.

What you need to do is modify the content of the bb variable. First clear the content then add the new items. Something like this:

private void toolStripJSONLoad_Click(object sender, EventArgs e)
{
    string tmp = File.ReadAllText(@"D:\path.txt", Encoding.UTF8);

    JavaScriptSerializer oSerializer = new JavaScriptSerializer();
    var tmpObj = oSerializer.Deserialize<List<BackupItem>>(tmp);
    bb.Clear();
    tmpObj.ForEach(o => bb.Add(o));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top