How to use BindingSource with .NET 4 Winforms

Posts   
 
    
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 01-Apr-2011 20:17:34   

Hi, I am trying to create a winform based test application for my management as a POC. I am trying to bind my DevExpress TextEditBox with the CustomerEntity of Northwind using BindingSource in Design time.

Can somebody guide me how to do it.

Using LLBLGen 3.1 Trial and VS2010 with .NET 4.0

I tried to follow the documentation but unable to do that, also did research on the forum but could't help. please excuse this newbie and may be silly question.

I appreciate if somebody can guide me the proper steps.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Apr-2011 04:22:25   

Hi Austin,

  1. Add EntityCollection class to the Toolbox. (Right click at toolbox then Choose items, and find the EntityCollection class.

  2. Drag an EntityCollection from the toolbox to the form. Then right click on that and configure it to find your EntityFactory (for instance, CustomerEntityFactory.

  3. Drag a BindingSource from the toolbox, then right click it and select your just added binding source as the source of the BindingSource.

  4. Drag your DataGrid or whatever control you want to bind to. Select your just added bindingSource as the data source of the grid.

  5. In any event of your form (a control's click, or the form's load) fetch the data into your entity collection instance.

That's it. If you want to view a live example, download a WinForms example from the LLBLGen site -> Customer Area. Please let us know if you need further assistance on this.

David Elizondo | LLBLGen Support Team
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 02-Apr-2011 16:52:57   

Thanks alot friend, its working now.

Friend, Now the query is that on the same form I have couple of textedit boxes and a grid. Now what I want to do is after adding a customer in the database how can i straightaway show that newly added customer in the grid. Can I achieve it by using the same Bindingsource, coz at the end grid is only display the Customers only.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Apr-2011 19:51:59   

When you save an entity this should be the flow:

  1. You have binded controls to your bindingSource, including your grid.
  2. You somehow indicate the bindingSource you want to add a new item. For instance:
_myCustomerBinding.AddNew();

you also can do this if you add a BindingNavigator in your form, this allows you to navigate through you bindingSource records. 3. When you added the item, you can save it. You need some Save button. Inside you verify all is ok and then:

OrderEntity orderToSave = (OrderEntity)_ordersBinder.Current;
// this is for SelfServicing
orderToSave.Save(true); 
// this is for Adapter, important to flag the save to refetch the entity
// new DataAccessAdapter.SaveEntity(orderToSave, true, true);
  1. Now that you saved, the involved collection is automatically updated, so is the bindingSource, and so the controls binded to the bindingSource, such your grid. So all is automatically.
David Elizondo | LLBLGen Support Team
Austinn
User
Posts: 38
Joined: 25-Aug-2008
# Posted on: 04-Apr-2011 21:22:17   

Thaks alot for your help it really worked.

One query again, when I am updating the textbox with a new value and then later trying to revert back the value by doing bindingsource.canceledit() the textbox value still remains updated, I want it to be changed back to the old value.

Can you guide me any solution.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Apr-2011 02:56:18   

You should look at where you are when you want to revert the value. Maybe it's too late. For instance, if you don't save the changes to DB, the value remains the same at DB, you could simply refetch it, or cancel the edit operation.

Remember that the bindingSource is in charge stay synced your underlying collection and the controls, it's not responsible for DB. Some controls trigger the update of the in-memory field. If I'm not mistaken when you lost focus from a textbox, the bindingSource update the value in the underlying collection.

For more Undo pattern behavior for BindingSource, I think you should check the binding source documentation for a suitable solution for you.

David Elizondo | LLBLGen Support Team