none
Why BindingSource RRS feed

  • Question

  • Ok, here is a real newbee question: What's the point of using a data binding source in Windows Form to bind a DataTable to a DataGridView, when it's possible to set the datasource on the DataGridView directly using the dgv.DataSource property? 

    Thanks

    Tonka

    Tuesday, April 19, 2011 1:35 PM

Answers

  • Hi Tonka78,

    The BindingSource offers the easiest way to navigate through records in a data source. And it is designed to simplify the process of binding controls to an underlying data source. 

    The new component BindingSource in C# 2.0 is the replacement for CurrencyManager. Although you can still use the CurrencyManager, the BindingSource component is the preferred way to bind your Windows Forms controls to data sources.

    It supports the bindung of control elements in a form, and can be seen as a link between a data source and a control element. It provides advantages over traditional data binding such as:

    Encapsulates the CurrencyManager functionality, and exposes the CurrencyManager events at design time.

    Interoperates with other data-related Windows Forms controls, i.e., the BindingNavigator and the DataGridView controls.

    The default property for the BindingSource class is DataSource. The default event is CurrentChanged. The BindingSource provides a number of methods, properties, and events such as CurrentItemChanged and DataSourceChanged, that allow for customization. The Position property gets or sets the index of the current item in the underlying list. The MoveLast() method changes the current value of the Position property to the index of the last item in the underlying data source, which is equal to the value of the Count property minus 1.

    For the sample, you can reference this article:

    http://www.codeproject.com/KB/grid/BindSourceBindingNavCS.aspx

     

    This question has also been discussed in the following article, so you can reference it:

    http://windowsclient.net/blogs/faqs/archive/2006/07/12/what-is-a-bindingsource-and-why-do-i-need-it.aspx

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 21, 2011 4:56 PM
    Moderator

All replies

  • hi Tonka

    You're right, the real reason imo is that too many people found the databinding and especially the currencymanager stuff too difficult, so they made a binding source to visually represent a binding. On top of that, you can link a bindingnavigator control to it, visually representing the currencymanager. But strictly spoken, you can do all that in code too.

    regards,

    Nico

    Tuesday, April 19, 2011 1:56 PM
  • The bindingsource provides advantages over directly binding a list to a grid. For example, many controls on your form can bind to the same bindingsource and changes will be kept in sync. Like if you had a master / detail scenario you could click a grid row and the detail (textboxes, etc) would automatically change. If you bound to a datatable you'd have to to build the refresh logic.
    Bob - www.crowcoder.com
    Tuesday, April 19, 2011 5:59 PM
  • @ Molku,

    with all respect, i know that sync logic is automatically there if your binding in code is consistent. By default, there's a bindingcontext created as a property of the form. By binding controls to a datasource, a currencymanager is created for each datasource, in that context. If the binding is done consistently, only one currencymanager per datasource is created. 


    Tuesday, April 19, 2011 6:18 PM
  • Agreed, BindingSource is just a convenience. I've directly used currencymanager before and it is more work. Every time I hear someone complain that "real" programmers don't use these shortcuts I cringe because programming is all about building some kind of product or solution that quickly and robustly meets a need. Its not about your code (gasp!). Remember, you could code an entire enterprise .net app with VI as your ide but I'll write 20 other apps while you do.

     


    Bob - www.crowcoder.com
    Wednesday, April 20, 2011 12:16 AM
  • @Molku

    whatever gets the job done is fine by me, i'm the last one to defend some religious standpoint about coding. Sometimes I use a bindingsource too, it is as you say, a convenience, and there's nothing wrong with conveniences.

    Wednesday, April 20, 2011 7:13 AM
  • Hi Tonka78,

    The BindingSource offers the easiest way to navigate through records in a data source. And it is designed to simplify the process of binding controls to an underlying data source. 

    The new component BindingSource in C# 2.0 is the replacement for CurrencyManager. Although you can still use the CurrencyManager, the BindingSource component is the preferred way to bind your Windows Forms controls to data sources.

    It supports the bindung of control elements in a form, and can be seen as a link between a data source and a control element. It provides advantages over traditional data binding such as:

    Encapsulates the CurrencyManager functionality, and exposes the CurrencyManager events at design time.

    Interoperates with other data-related Windows Forms controls, i.e., the BindingNavigator and the DataGridView controls.

    The default property for the BindingSource class is DataSource. The default event is CurrentChanged. The BindingSource provides a number of methods, properties, and events such as CurrentItemChanged and DataSourceChanged, that allow for customization. The Position property gets or sets the index of the current item in the underlying list. The MoveLast() method changes the current value of the Position property to the index of the last item in the underlying data source, which is equal to the value of the Count property minus 1.

    For the sample, you can reference this article:

    http://www.codeproject.com/KB/grid/BindSourceBindingNavCS.aspx

     

    This question has also been discussed in the following article, so you can reference it:

    http://windowsclient.net/blogs/faqs/archive/2006/07/12/what-is-a-bindingsource-and-why-do-i-need-it.aspx

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 21, 2011 4:56 PM
    Moderator
  • Thanks for all the answers, now I got a clearer picture of the binding source :)
    Friday, April 29, 2011 8:40 AM
  • You're welcome.

     


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 29, 2011 12:41 PM
    Moderator