Question

I am having difficulty getting my complex object databinding to work properly. I have 3 DataGridViews on a form and 1 binding source. The BindingSource.DataSource is the FileMoveProcesses object and the DataMember is the FileMoveProcess object

public class FileMoveProcesses
{
    public List<FileMoveProcess> Processes { get; set; }
}

public class FileMoveProcess
{
    public string Name { get; set; }
    public bool Disabled { get; set; }
    public FileMoveProcessDetails SourceDetails { get; set; }
    public FileMoveProcessDetails DestinationDetails { get; set; }
}

There are 2 other DataGridViews that have DataSource of the BindingSource and the DataMembers are SourceDetails and DestinationDetails respectively. This work fine if I have data already available in the xml file for these detail items as you can see below. The edit process works just fine the updates are serialized as you would expect.

Here is a pic with data loading and editable

The problem comes in when I try to add a new FileMoveProcess. The far left DataGridView containing the FileMoveProcess will save correctly but when I attempt to add the SourceDetails and DestinationDetails data in the datagridview they fail to create the FileMoveProcessDetails objects for the new FileMoveProcess so it's not available to write to the xml file.

Data Failed to be added to the binding source

What did I miss?

TIA

PS the ComboBoxes are Enum databinding so the data is available for the new item.

Was it helpful?

Solution 2

I think you must add your new object to the bindingSource yourself. I created a simple version of your project, with only 2 DGV. I also set the Data Source Update Mode to "Never" in my dataGridView2 -> DataBindings -> Advanced

When I write a new FileMoveProcessDetails (in the right grid) I can save it clicking a button and calling this code:

private void button1_Click(object sender, EventArgs e)
{
    var myCurrentRow = dataGridView2.Rows[dataGridView2.CurrentRow.Index];
    var fmpd = myCurrentRow.DataBoundItem;
    var pp = (FileMoveProcess)processesBindingSource.Current;
    pp.SourceDetails = (FileMoveProcessDetails)fmpd;
}

It's very raw... but the BindingSource is saved and I don't lose my new FileMoveProcessDetails it when I navigate to other FileMoveProcess and back.

OTHER TIPS

I've encoutered the same problem and relized that all you have to do is to create new instance of List collecion in the default constructor. :)

public class FileMoveProcesses
{
    public FileMoveProcesses()
    {
        Processes = new List<FileMoveProcess>();
    }

    public List<FileMoveProcess> Processes { get; set; }
}

public class FileMoveProcess
{
    public string Name { get; set; }
    public bool Disabled { get; set; }
    public FileMoveProcessDetails SourceDetails { get; set; }
    public FileMoveProcessDetails DestinationDetails { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top