7

I have a Custom Control (Windows Form) that is a lookup text box. A property on the Control is Current Selection which is a Custom Object containing "Identifier", "Code" and "Description". This property is Databound using a BindingSource.

Displaying the information works great. On the other hand regardless of whether I set the Update to OnValidate or OnValueChange it never updates the BindingSource. Is there something I'm missing to get this to auto update?

private System.Windows.Forms.BindingSource buildPlanComponentDataBindingSource;

    public void LoadBuildPlan(string itemNumber)
    {
        var buildPlanComponents = BuildPlan.LoadBuildPlanComponents(itemNumber, AutomaticPrice);
        buildPlanComponentDataBindingSource.DataSource = buildPlanComponents;
        AssemblyNumber = itemNumber;
    }




[Bindable(true)]
[DefaultValue(null)]
public ILookupSelection CurrentSelection
{
    get
    {
        if (currentSelection == null)
            currentSelection = new LookupSelection {Code = txtLookup.Text};

        return currentSelection;
    }

    set
    {
        if (value == null) return;

        currentSelection = value;

        SetText(currentSelection, DisplayText);
        SetDescription(currentSelection, DisplayDescription);
    }
}
2
  • Could you show us the code where you create the data binding? Commented Mar 3, 2009 at 16:44
  • Thanks, your question was very helpful. For some reason, the MSDN tutorial left out the [Bindable(true)] attribute from its tutorial. That's an important detail! Commented May 13, 2016 at 15:21

3 Answers 3

2

Implementing INotifyPropertyChanged seems to be the solution!

    #region IPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, e);
        }
    }

    #endregion
Sign up to request clarification or add additional context in comments.

1 Comment

IPropertyChanged should read INotifyPropertyChanged. I copied the code and got fooled by this detail when defining the inheritance. Otherwise this is great, +1
0

Displaying the information works great. On the other hand regardless of whether I set the Update to OnValidate or OnValueChange it never updates the BindingSource.

Looking at your code I'm actually not sure of this. In your set, you test for null and abandon; if the data actually contains null (which is what you're describing) your control will be out of synch. I wonder if perhaps that check is masking the underlying problem.

2 Comments

The only time that is really ever NULL is when the control is initially created. Otherwise this will only be set when a Selection is made which contains a value.
If that's the case then I would recommend you use a different flag to test, other than value == null. Databinding to properties is a back and forth process ... at any rate, I would clean this bit up, but I'm also not positive it'll help your problem in the end.
0

Maybe you need to cause the DataBinding to write its value for each control whose value you are setting this way?

Assuming one data binding for a textbox named txtMySetValue:

txtMySetValue.DataBindings[0].WriteValue();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.