3
votes

Using WinForms and data bindings.

I have a form containing a BindingSource component and I have set the DataSource property from the designer to a class:

public class MyClass
{
    public string MyString {get;set;}
}

Now, how do I get the MyClass object assigned to the BindingSource from source code?

I've tried casting the DataSource property of the BindingSource to MyClass, not working.

Additional notes

My problem seems to be that I set the DataSource from the WinForms Designer.
The DataSource is then set to the type MyClass and not an actual object.

So, is there an object created that I can access and modify from code so that values from the bounded object shows on the form's controls ?

Thank you.

1

1 Answers

1
votes

To add an object data source in a WinForms application, from the menu choose Data > Add New Data Source. In the Data Source Configuration Wizard choose Object and click Next. Select the class you wish to use as a data source and click Finish. The public properties of the class should now appear in the Data Sources window.

Then after you do that create an instance of the class and assign it to the DataSource property. For example:

private void Form1_Load(object sender, EventArgs e)
{
    MyClass myClass = new MyClass()
    {
        MyString = "aaaa"
    };

    myClassBindingSource.DataSource = myClass;
}