When setting the DataSource property of an object, when do you use the type against the instance?

Question

What is the difference between setting [...].DataSource an object instance and a class type? I have seen both methods that are used in our code base, and I am trying to bow my head to the fact that this is somehow in any of these cases.

Example

how

 object1.DataSource = typeof(SomeClass); 

differ from

 object2.DataSource = getSomeObject(); 

Further inquiry

Also, in the first case, if I set the DataSource as a class type, what happens if this class is a base class? Is data binding associated with properties that exist only in classes that come from the base class? Or does data binding only work with members of class type I that I set the DataSource to?

It’s hard for me to write down my Google searches to give me an answer to this question. And this is because this material is complex, and I just do not formulate it correctly, or I do not quite understand some basics of data binding in C #. Can I get some help pointing in the right direction here? Thanks!

+8
c # data-binding winforms datasource bindingsource
source share
2 answers

When you set the BindingSource.DataSource property to a type, the control is bound to an empty IBindingList with elements of that type. Thus, the data source will initially have no elements. If, on the other hand, you set the DataSource to a set of elements, the data source will be bound to an IBindingList with these elements.

Thus, type assignment gives you an empty list of items, and collection assignment gives you a list with items from the collection.

If you assign a base type, you get an empty list of elements of the base type. Data binding does not "know" about any derived classes.

+3
source share

If you specify a type of data source of a type, you determine which type of type you will process later. This will help to associate the properties of this object with the elements in the designer.

Setting the value is needed at a later stage to determine which data will actually be displayed.

Edit: And you can only access properties that belong to the class you are processing, and not to the parent classes.

+2
source share

All Articles