1

I am attempting to create a databinding between a numericupdown and an array element. In my form I have tried creating the binding shown below, but it doesn't seem to work. Any help would be appreciated.

Binding:

nudTest.DataBindings.Add("Value", eac.ESettings.HsvArray[0], "", false,DataSourceUpdateMode.OnPropertyChanged);

Array:

public class ESettings : INotifyPropertyChanged
{

    private int[] hsvArray = new int[6];

    public event PropertyChangedEventHandler PropertyChanged;

          [XmlIgnore]
    public bool PrgVarIsDirty
    {
        get { return prgVarIsDirty; }
        set
        {
            prgVarIsDirty = value;
            OnPropertyChanged("PrgVarIsDirty");
        }
    }

    public int[] HsvArray
    {
        get { return hsvArray; }
        set
        {
            if (value != hsvArray)
            {
                prgVarIsDirty = true;
                hsvArray = value;
                OnPropertyChanged("HsvArray");
            }

        }
    }


    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
3
  • 1
    WinForms does not support such data binding Commented Jul 11, 2016 at 19:59
  • Is there a different way I should be doing this or am I just out of luck? Commented Jul 11, 2016 at 20:23
  • When you want to bind a control to an element of an array, instead of trying to bind directly to the element, bind control to the array, and then set the Position of CurrencyManager to the index of that element in the array. Commented Jul 12, 2016 at 12:36

1 Answer 1

2

When you want to bind a control to an element of an array, instead of trying to bind directly to the element, bind control to the array, and then set the Position of CurrencyManager to the index of that element in the array.

For example, below code binds the NumericUpDown to array and shows 30, the element at index 2:

int[] array = new int[] { 10, 20, 30, 40 };
private void Form1_Load(object sender, EventArgs e)
{
    this.numericUpDown1.DataBindings.Add("Value", array, "");
    ((BindingManagerBase)this.numericUpDown1.BindingContext[array]).Position = 2;
}

The same binding could be done using BindingSource. It's enough to set array as DataSource of the binding source and use the binding source for data binding. Then to show a specific element, set Position of BindingSource.

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

8 Comments

This doesn't work great if the model object should be supplied from another BindingSource, for example to display array elements in multiple columns of a DataGridView, where each array is a DataGridView row.
@BenVoigt The example above is a workaround for special case: "Data binding a control to an element of an array at specific index". DataGridView supports complex databinding. As an example example, assuming you have a list of categories, each of which has a list of products and you want to show list of products in a DataGridView, for the selected category, then it's enough to use two binding sources, one for categories and another one for the relation/navigation property.
For more complex situation, if databinding doesn't work easily, you can define a custom type descriptor. For example, take a look at this: stackoverflow.com/questions/65794579/… or this one: stackoverflow.com/questions/65925733/… The examples are not relevant to this question, but they show how can you use power of type descriptor for databinding.
Yeah that's the path I am planning to go down. It's a little complicated by the fact that I can't just associate a PropertyDescriptor with the DataGridView column, the column requires a DataMember string and insists on looking up that string with a type descriptor. Wish the architects would have made extending it in this way a bit easier.
My particular case is that the user can define new columns via a configuration file. The data for the custom columns is in a Dictionary on the data class. I could build the grid contents without databinding but then I'd lose automatic refresh of changes to the predefined properties. For simplicity, consider one predefined column "Status" and multiple custom columns. The Status value will change many times while the row is displayed, none of the other predefined or custom columns will change after the row is added.
|

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.