none
Refresh DataBinding

    Question

  • 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

    Wednesday, August 16, 2006 3:12 PM

Answers

  • 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.

    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

    Monday, August 21, 2006 1:50 PM
  • Ahh... ok. That's annoying. Good job solving the problem.
    Tuesday, August 22, 2006 3:03 PM

All replies

  • 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

    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.

    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 should refresh the data on the controls attached to the DataSet.

    Curtis

    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 dataset members 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

    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.

    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,

    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

    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.

    Monday, August 21, 2006 2:07 PM
  • Ahh... ok. That's annoying. Good job solving the problem.
    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();
    }


    Tuesday, November 14, 2006 1:47 AM
  • try this (maybe too late Stick out tongue)

     

    myBindingSource.ResetBindings(False)

    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 containers on 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
    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 could not just 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 by ENTBedford Saturday, August 08, 2009 1:49 PM Nanny made me rename the Gender field from its origianl ____
    • Proposed as answer by Ian Arhip Tuesday, December 03, 2013 5:16 PM
    Saturday, August 08, 2009 1:45 PM
  • This is a really old thread but I used it to solve my problem which was identical to the OP. There was an unanswered issue in this thread that I solved which made the whole process a lot easier.

    I had a data source called myDataSource and a ListBox called myListBox. As the original poster did, I changed data in myDataSource in a different thread. The data source obviously doesn't update the controls bound to it so myListBox's contents remains unchanged.

    I called myDataSource.RefreshBindings(false) in the other thread which doesn't work because I figured out that data sources won't complete non-threadsafe operations on controls. Instead, I did the following:

    delegate void myDelegate();
    
    private void changeDataSourceData() //this is executed in a different thread
    {
          //do data source operations here
          
          myDelegate d = new myDelegate(updateMyList);
          myListBox.Invoke(d);
    }
    
    private void updateMyList()
    {
          myDataSource.ResetBindings(); //This is now executed in the main program thread which is the same thread the list box was created in and is now threadsafe
    }

    Problem solved :)
    • Proposed as answer by st1nkbutt Wednesday, March 05, 2014 9:35 PM
    • Unproposed as answer by st1nkbutt Wednesday, March 05, 2014 9:35 PM
    • Proposed as answer by st1nkbutt Wednesday, March 05, 2014 9:35 PM
    Friday, November 09, 2012 1:54 AM
  • Here is an extension method that refreshes all bindings on a Form

            public static void ResetDatasourceBindings(this Form source)
            {
                FieldInfo[] fields = source.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                foreach (var field in fields)
                {
                    if (field.FieldType == typeof(BindingSource))
                    {
                        BindingSource binding = (field.GetValue(source) as BindingSource);
                        if (binding != null)
                        {
                            binding.ResetBindings(false);
                        }
                    }
                }
            }

    Wednesday, March 05, 2014 9:36 PM