unbinding bindingsource
Asked Answered
Q

4

6

I am using a bindingsource in my windows forms application to populate some textboxes etc in my view. Binding works OK but how can I unsubscribe my bindingSource from from my object?

bindingSource.DataSource = new Foo();//OK
bindingSource.DataSource = null;//Not ok

If I try to unbind by setting data = null I get an exception:

System.ArgumentException : Cannot bind to the property or column Bar on the DataSource. Parameter name: dataMember

I don't want to remove all bindings to my controls (i have a lot) but would like to suspend binding as long as the bindingSource has no data....

I found a workaround like this bindingSource.DataSource = typeof(Foo); but is this THE way?

Quigley answered 29/6, 2011 at 15:30 Comment(0)
S
5

The typeof "workaround" is actually what the windows forms designer does when you set the BindingSource's DataSource in the PropertyGrid, and select a type from "Project data sources".

Look at the generated code in the *.designer.cs file for your form.

We use this "trick" in one of our products, and it has worked well for many years now.

Regards

Seacock answered 12/8, 2011 at 22:10 Comment(0)
G
0

I am not aware of a .Data property for the BindingSource object, but there is a .DataSource property, which can be set to null:

bindingSource.DataSource = null;

This releases the binding source from the data. However, looking at the reference for BindingSource.DataSource:

DataSource property             List results
----------------------------    -------------------------------------------
null with DataMember set        Not supported, raises ArgumentException.

If you're using a DataMember, you can't set the DataSource to null without an exception.

Unfortunately I don't know whether your workaround is a proper way of doing it, but at least now we know that you can't simply bind to null when a DataMember is set.

Genous answered 29/6, 2011 at 15:48 Comment(0)
Q
0

mrlucmorin gave you correct answer. It is working and it is the correct way of handling such situation.

However it won't quite work if your DataSource is of DataTable type. In such case you might want to play with bs.RaiseListChangedEvents = false; before nulling the BindingSource.DataSource, and set it to true after you assign new DataSource. Right after you set it to true, don't forget to reset the bindings with bs.ResetBindings(true);

Be aware that this might cause leaving your databound controls with 'old' data in them.

Quintie answered 15/12, 2012 at 18:22 Comment(0)
C
0

When using typeof as "empty" value for the DataSource, you can test for it like this:

private void BindingSource_DataSourceChanged(object sender, EventArgs e)
{
    DataSource dataSource = ((BindingSource)sender).DataSource;

    if (dataSource is Type t && t == typeof(MyModel))
    {
        lblEmpty.Visible = true;
        pnlDetails.Visible = false;
    }
    else
    {
        lblEmpty.Visible = false;
        pnlDetails.Visible = true;
    }
}

This way you can conditionally hide or show an "empty" message in the UI, in a simple way.

Cageling answered 10/10, 2021 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.