开发者

unbinding bindingsource

开发者 https://www.devze.com 2023-03-16 11:13 出处:网络
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 un开发者_JS百科subscribe my bindingSource from from my object?

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 un开发者_JS百科subscribe 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?


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消