Hi,
How can I manually trigger a databinding to refresh my controls binded to a datasource.
(DataSource have been updated behind the scene.)
Similar to Page.DataBind() in ASP .NE |
| Etienne Lefrancois Wednesday, August 16, 2006 3:12 PM |
To Reset the contents of every control and change what it is displaying manually run this code (in c# but vb.net conversion is easy):
foreach (Control ctl in this.Controls) { ctl.ResetBindings(); }
Check out the CurrencyManager documentation on MSDN. Also take a look at the BindingContext information as well.
To just call Refresh on every CurrencyManager on your form use this code:
foreach (BindingManagerBase ctl in this.BindingContext) { if (ctl is CurrencyManager) { CurrencyManager cm = (CurrencyManager)ctl; cm.Refresh(); } }
Hope this helps. |
| Curtis Gray Wednesday, August 16, 2006 8:00 PM |
|
I gave up and I implemented my own derived class from Binding.
this class implement a fonctions RefreshBinding(). |
|
Wow. Did it really take all that code? the RefreshBinding code didn't work, eh? Man. That's surprising.
Curtis |
| Curtis Gray Monday, August 21, 2006 1:50 PM |
Ahh... ok. That's annoying. Good job solving the problem. |
| Curtis Gray Tuesday, August 22, 2006 3:03 PM |
I presume you don't want to use Page.DataBind. Is there a reason for not using Page.DataBind?
If it is because you want to isolate the binding action to a certain set of controls you can use an ASP.NET controls container such as a Panel or a PlaceHolder. Place the controls within the container, then call DataBind on the container. It will call DataBind on all other controls in the container.
Or you could create your own container class and add the controls to it and call DataBind on every control in the container yourself.
Make sense?
Curtis |
| Curtis Gray Wednesday, August 16, 2006 3:56 PM |
Creating my own container class for all binded controls is a good solution. thanks,
But I'm using winform controls, so what fonctions or whatever I can call on all controls to refresh values with DataSource values.
DataBind doesn't exist on winform controls.
thanks. |
| Etienne Lefrancois Wednesday, August 16, 2006 5:28 PM |
Ohhhhh.... ok. I just "assumed" it was a WebForm.
Databinding in WinForms is "so straightforward", according to MS, that they didn't include a DataBind method for controls. Instead, you can do various things such as fire an event when you want to rebind the data and then Fill the dataset(s) that you want to refresh. Fill'ing the DataSet shouldrefresh the data on the controls attached to the DataSet.
Curtis |
| Curtis Gray Wednesday, August 16, 2006 6:32 PM |
OK I sea.
Here some more explainations about the problem.
My controls are binded to dataset properties. I Have a Type Dataset that contain a DataTable with a single row.
Each member of this row are binded to winform.Labels
I change de DataSetValues, but the controls are not refresh automatically when the datasetmembers change.
Some event or fonctions need to be call to let the form know that the DataSource Changed behind the scene.
So I plann to keep a collection of binded control, or a collection of Binding, and call some fonctions to trigger a rebind. or simply call event that I don't know the existance.
** One Point, if I minimise my form, when I re-open it, the binding event is fire, so what I did is I tried to call on my form or controls fonctions like Paint(), refresh(), Invalidate() etc... but It didn't work. :-(
I don't know what to do now, I'm so tired to work on this problem. I can't believe there is no fonction like RefreshBinding.... anyway,
thanks buddy,
..Etienne |
| Etienne Lefrancois Wednesday, August 16, 2006 7:09 PM |
To Reset the contents of every control and change what it is displaying manually run this code (in c# but vb.net conversion is easy):
foreach (Control ctl in this.Controls) { ctl.ResetBindings(); }
Check out the CurrencyManager documentation on MSDN. Also take a look at the BindingContext information as well.
To just call Refresh on every CurrencyManager on your form use this code:
foreach (BindingManagerBase ctl in this.BindingContext) { if (ctl is CurrencyManager) { CurrencyManager cm = (CurrencyManager)ctl; cm.Refresh(); } }
Hope this helps. |
| Curtis Gray Wednesday, August 16, 2006 8:00 PM |
It's like nothing is simple here... :-)
I gave up and I implemented my own derived class from Binding.
this class implement a fonctions RefreshBinding().
this fonction, behind the scene,force the re-population of the control propertie with the new dataSource value using the .NET reflection.
public void RefreshBinding()
{
// Get DataSOurce Current Value
PropertyInfo pi = DataSource.GetType().GetProperty(m_dataMemberName);
object obj = pi.GetValue(this.DataSource, null);
// Set Value to the Control binded to this dataSource
PropertyInfo pi2 = this.Control.GetType().GetProperty(this.PropertyName);
if(m_validator != null)
{
// A Validator exist. use it to transform displayable value
pi2.SetValue(this.Control, m_validator.Format(Convert.ToString(obj)), null);
}
else
{
// no validation is requiered. juste display value
pi2.SetValue(this.Control, Convert.ToString(obj), null);
}
}
thanks Curtis, |
| Etienne Lefrancois Thursday, August 17, 2006 7:47 PM |
|
I gave up and I implemented my own derived class from Binding.
this class implement a fonctions RefreshBinding(). |
|
Wow. Did it really take all that code? the RefreshBinding code didn't work, eh? Man. That's surprising.
Curtis |
| Curtis Gray Monday, August 21, 2006 1:50 PM |
RefreshBinding doesn't exist on PropertyManager casll obtain from BindingContext[].
RefreshBinding Only exist on CurrencyManager object.
Framework 2.0 handle this problem. |
| Etienne Lefrancois Monday, August 21, 2006 2:07 PM |
Ahh... ok. That's annoying. Good job solving the problem. |
| Curtis Gray Tuesday, August 22, 2006 3:03 PM |
| Etienne Lefrancois wrote: | RefreshBinding doesn't exist on PropertyManager casll obtain from BindingContext[].
RefreshBinding Only exist on CurrencyManager object.
Framework 2.0 handle this problem. |
|
I spent a long time trying to figure out a way to do this
myself. In 2.0 a control's databindings
can be refreshed this way.
myControl.DataBindings["Text"].ReadValue();
Or to refresh all the bindings of a control
foreach (Binding b in myControl.DataBindings) { b.ReadValue(); }
|
| dmp1ce Tuesday, November 14, 2006 1:47 AM |
try this (maybe too late )
myBindingSource.ResetBindings( False) |
| zaracato Thursday, October 02, 2008 10:40 PM |
I recently ran into the same problem. This is what I come up with...
It's recursive because I had some GroupBox containerson my form.
Private Sub RefreshBindings()
For Each ctl As Control In Me.Controls
For Each b As Binding In ctl.DataBindings
b.ReadValue()
Next
RefreshContainer(ctl)
Next
End Sub
Private Sub RefreshContainer(ByVal objContainer As Control)
For Each ctl As Control In objContainer.Controls
For Each b As Binding In ctl.DataBindings
b.ReadValue()
Next
RefreshContainer(ctl)
Next
End Sub
|
| Rick Coker Friday, July 24, 2009 3:34 PM |
A bit late, but I had a similar question. I had to retrieve and display database information based on user input. Displayed info had to change to reflect new user input.
I couldnotjust refresh as the binding had to alter based on user input (different data if male/female)
I found it easiest just to clear bindings and rebind, which seems to bring in the new data OK. This might be more difficult if there are multiple bindings, but I only have one per control.
Data in Agedisabilitypercent with fields Age, Male, Female 2 user-completed controls, Age and Gender 1 control to show age and gender appropriate % disability, Agepercent. Might help someone....:
AgedisabilitypercentBindingSource.Filter = "Age = " & Age.Text AgePercent.DataBindings.Clear() AgePercent.DataBindings.Add(New Binding("Text", AgedisabilitypercentBindingSource, IIf Gender.Text = "M", "Male", "Female")))
- Edited byENTBedford Saturday, August 08, 2009 1:49 PMNanny made me rename the Gender field from its origianl ____
-
|
| ENTBedford Saturday, August 08, 2009 1:45 PM |