Bindingsource.datasource Error in desing

  • Thread starter Alhambra Eidos Kiquenet
  • Start date
A

Alhambra Eidos Kiquenet

Hi misters,


I used the Data Sources window to drop a custom object onto the designer
surface. It created a BindingSource and BindingNavigator as a result.

When I open the Properties window on the BindingSource, I see DataSource
property set to the object I created.



However, when I try to dropdown the DataSource property, I get the message:

"Object reference not set to an instance of an object". The title of the
messagebox is "Microsoft Visual Studio".

So I cannot change the DataSource, I went into the designer file and
removed the DataSource property of the BindingSource and went back into the
designer. It showed the DataSource as (none) as I expected.

But the same error message occurs when I try to change the property with the
dropdown box.

What is going on? Any help will be very grateful

Thanks in advance, regards
 
M

Morten Wennevik [C# MVP]

Hi Alhambra,

I'm suspecting the BindingSource is created using a Type as DataSource
instead of an object instance. Designer created BindingSources may need some
manual code to be properly initialized.

I don't know too much about the designer features as I never use it, but it
is easy to create (and in my opinion far easier to use) in code.

Sample code:

public Form1()
{
InitializeComponent();

BindingSource source = new BindingSource(new MyClass(), "MyListProperty");
listBox1.DataSource = source;
listBox1.DisplayMember = "MyProperty";
textBox1.DataBindings.Add("Text", source, "MyProperty");
}

class MyClass
{
private List<MySubClass> _myListProperty = new List<MySubClass>();

public List<MySubClass> MyListProperty
{
get { return _myListProperty; }
set { _myListProperty = value; }
}

public MyClass()
{
MyListProperty.Add(new MySubClass("Hello"));
MyListProperty.Add(new MySubClass("And"));
MyListProperty.Add(new MySubClass("Goodbye"));
}
}

class MySubClass
{
private string myProperty;

public string MyProperty
{
get { return myProperty; }
set { myProperty = value; }
}

public MySubClass(string text)
{
MyProperty = text;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top