8

Question

What is the difference between setting a [...].DataSource to an instance of an object vs. the type of a class? I have seen both methods in use in our codebase, and I'm trying to wrap my head around why it is one way or the other in any of these cases.

Example

How does

object1.DataSource = typeof(SomeClass);

differ from

object2.DataSource = getSomeObject();

Further inquery

Also, in the first case, if I set a DataSource to be the type of a class, what happens if that class is a base class? Does the data binding work on properties that only exist in classes that descend from the base class? Or does the data binding only work on the class members of the type of the class I set the DataSource to?

I'm having a hard time wording my Google search queries to give me an answer to this question. And that is either because this stuff is complicated and I'm just not wording it right, or I do not quite understand some of the fundamentals of data binding in C#. Could I get some help getting pointed in the right direction here? Thanks!

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Jake Smith
  • 2,332
  • 1
  • 30
  • 68
  • 1
    @MartinLiversage how do you know that this is winforms and not some other UI paradigm? – Servy Aug 18 '14 at 17:29
  • @Servy, I agree, it could have been. But he is right, it is `WinForms`. I should have put that tag in. – Jake Smith Aug 18 '14 at 17:30
  • @Servy: I highly doubt that this question could have been about another UI paradigm but I think that adding the tag for the UI paradigm helps attract good answers. – Martin Liversage Aug 18 '14 at 17:39
  • @MartinLiversage And why couldn't the question be about another UI paradigm? Adding the tag for the *wrong* UI paradigm will attract plenty of attention, but it can also attract *incorrect or misleading answers* if the solution is paradigm dependent, which is often the case. – Servy Aug 18 '14 at 17:41
  • 1
    @Servy: The wording of the question made it obvious to me that this question was about Windows Forms, and it turns out that I was right. If you see a question with C# code but without a language tag you would probably not object to somebody editing the tag in? Editing posts on SO I have not written myself is challenging but my goal is to improve, not make mistakes. And if I make a mistake the collaborative nature of SO will hopefully quickly undo what I did. – Martin Liversage Aug 18 '14 at 17:52
  • @MartinLiversage What in the question is winform specific, as opposed to say applying to ASP or WPF? All of the concepts used in the question apply equally to those other paradigms. – Servy Aug 18 '14 at 17:55
  • 1
    @Servy: Please look at my answer. The [bindinglist] tag and the `DataSource` property and the fact that you can assign a type to the `DataSource` property. This makes it obvious that the question is about the `System.Windows.Forms.BindingList.DataSource` property. There is not such property with that name where you can assign a type in WPF nor in ASP.NET. It troubles me a bit that I made an edit that the author of the question seems to approve and you still want to challenge this edit? – Martin Liversage Aug 18 '14 at 18:01
  • 1
    @MartinLiversage There is no "bindinglist" tag. Nothing in the question mentions the use of "bindinglist" as opposed to any other type with a `DataSource` property. You are the only person who has ever mentioned `BindingList`. I'm concerned because if you're in the habit of adding tags to questions in which that tag may well not apply then the fact that you happened to guess right this once doesn't mean you'll guess right every single other time you make such edits, and adding the wrong paradigm's tag can have significant negative consequences. – Servy Aug 18 '14 at 18:05
  • 1
    @Servy: Sorry, I made a typo in the comment. Please replace "bindinglist" with "bindingsource". Hopefully it makes mores sense then. And if you believe that I have added wrong tags to questions then please let me know so I can learn from my mistakes. However, simply insinuating that I do that on regular basis is not very constructive in my opinion. – Martin Liversage Aug 18 '14 at 18:15

2 Answers2

4

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

So assigning a type gives you an empty list of items while assigning a collection gives you a list with items from the collection.

If you assign a base type you get an empty list of base type items. The data binding does not "know" about any derived classes.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • Or you get an exception: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. – Steve Wellens Aug 18 '14 at 17:33
  • 1
    Does your answer only apply to setting the DataSource of a control? I've run across places where we "new up" a `BindingSource` object and set the `DataSource` property of that rather than go directly to the `DataSource` of the control itself. Sorry if this is a dumb question... – Jake Smith Aug 18 '14 at 17:37
  • 1
    @JakeSmith: I am referring to the `BindingSource` class. I am not sure that all controls necessarily will implement the same behavior in their specific `DataSource` properties. – Martin Liversage Aug 18 '14 at 17:45
  • Okay, great! So really the idea is that setting the `DataSource` to a type will help you make data binding decisions using the Visual Studio Designer interface rather than writing a bunch of code and having to run/debug it to see if it is done properly? And then like you said, at some point, that DataSource has to be provided an actual instance of said type. – Jake Smith Aug 18 '14 at 18:46
2

If you set the datasource to be of a type you define what kind of type you will be handling later. This will help in binding properties from that object to elements in the designer.

Setting the value is needed at a later stage to define what data will actually be shown.

Edit: And you can only access properties that is on the class you are handling, not any parent classes.

helgeheldre
  • 1,081
  • 11
  • 20