I have a UserControl with multiple fields that I would like to have bound to a BindingSource. I would also like the UserControl to expose some BindingSource property so that it can be dropped on a Form and be bound to the BindingSource on the form. Is there an easy way to do this? I realize that I can rebind all of the controls of the UserControl in its BindSource setter. But this seems wrong. Is there some BindingSource Proxy that will let me link the BindingSource in the user control to the BindingSource in the form?
相关问题
- How do I bind a DataGridViewComboBoxColumn to a pr
- Partial Form Class C# - Only display code view for
- Can we add four protocols to ServicePointManager.S
- How to properly handle form closing when using bac
- Filter Datagridview rows using TextBox
相关文章
- Sort TreeView Automatically Upon Adding Nodes
- Where does this quality loss on Images come from?
- Missing partial modifier on declaration of type
- PropertyGrid - Possible to have a file/directory s
- Why do base Windows Forms form class with generic
- How to handle the TextChanged event only when the
- Difference between SuspendLayout and BeginUpdate
- Adding Combobox to DataGridView Headers
If you wanted to do this all automatically you could look for the binding source from the parent form in the load event of your user control or something like that...
I know it's a late answer; however, it might be useful to someone else reading this post.
I have controls on a
UserControl
that are data-bound. I need to have aBindingSource
on theUserControl
in order to be able to bind the controls at design time. The "real"BindingSource
, however, sits on theForm
. In other words, the controls on theUserControl
should behave as if they were sitting directly on the form (or on aContainerControl
on the form).The idea behind this solution is to watch for the
DataSourceChanged
event of the "real"BindingSource
and to assign itsDataSource
to the localBindingSource
when it changes. In order to find the "real"BindingSource
I let theForm
(orControl
) containing it implement the following interface:We can watch for the
ParentChanged
event of a control in order to know when it has been added to aForm
or aContainerControl
. The problem here is that thisContainerControl
itself might not have been added to theForm
(or anotherContainerControl
) yet at this time. In this case we subscribe to theParentChanged
event of the last parent we find in the parents chain and wait until this last parent has been added, an so on, until we find aControl
orForm
implementingIDataBound
. When aIDataBound
has been found, we subscribe to theDataSourceChanged
event of itsBindingSource
.As per your question, I can hardly get what you intend to do. Thus I will try my best to provide you with, I hope, interesting information on that matter.
First, let's consider the following UserControl in a Customer management software project.
Second, let's consider the following Form which should be your Customer management form.
If you're expecting to use CustomerManagementUserControl.DataSource property from within the Property window, please consider adding the following on top of your property definition.
This is one way of doing what I guess you might want to do. On the other hand, if what you wish to do is to get the as most abstract as possible by setting a different type of object as your UserControl.BindingSource.DataSource property, then you will have to write a method which could detect the type of the object passed, then binding the properties accordingly. A nice way you could go, perhaps, is by Reflection, if you're comfortable working with it. In any possible way you may imagine working with such polymorphism features, you will have to write yourself an interface that all of your bindable objects will have to implement. This way, you will avoid unknown property names, and when will come the time to bind your UserControl's controls, you will be able to bind the correct property to the correct control and so forth.
Let's try the following:
Considering the above code, you could use the DataSource property of your UserControl to bind with an IEntity, so your property could like like this.
That said, if you wish to push even further, you could just expose your UserControl's controls DataBindings properties in order to set them on design-time. Considering this, you will want to expose your BindingSource as a public property either so that you may set it on design-time too, then choose your DataMember from this BindinSource.
I hope this helps you both a little or at least, give you some tracks for further searchings.
If you assign the same object reference as the datasource on two bindingsources, the controls will not be updated consistently on the second bindingsource. Possibly, a compromise to the choices above is the following:
VS
designer to set the bindings to the controls.designer.vb
up in the code editor. Search for all the"DataBindings.Add"
lines that were created by the designer. Copy them all to notepad.There is less typing and less typos because the
VS
designer is creating all those literal text property names.