Binding to a single entity

Posts   
 
    
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 29-Nov-2004 20:22:20   

Will it ever be possible to bind a control to a single entity vs an EntityCollection? If not (obviously I haven't tried this yet), should I be able to bind to an EntityCollection for the datasource, and then specify a row index and column name for the datamember?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 29-Nov-2004 21:13:45   

Complex databinding controls require IList implementing datasources. Typically this is true for list-oriented controls like grids, combo-boxes, listboxes etc. etc.

Simple databinding controls like textboxes, require just a property on an object.

So, you can bind an entity collection to a grid, and bind the current entity's properties to textboxes for example.

Frans Bouma | Lead developer LLBLGen Pro
Andrei
User
Posts: 7
Joined: 19-Jan-2007
# Posted on: 08-Mar-2007 04:10:12   

So, how do you do it? I bound a few text boxes to an entity type (I have AlphaEntity class with LastName, FirstName properties which I bound to those text boxes), then after initialization, I assigned the datasource of my binding source to an acutal entity object. It did not work, here is my code:

        m_AlphaEntity = new AlphaEntity(hAlpha);
        if (m_AlphaEntity.IsNew)
            throw new System.ApplicationException("The hAlphaKey " + hAlpha.ToString() + " not found.");

        // bind the controls on the form:
        this.alphaEntityBindingSource.DataSource = m_AlphaEntity;


        // adding the following code did not help to display the LastName and the FirstName of the current m_AlphaEntity either:
        this.alphaEntityBindingSource.ResetBindings(true);
        this.alphaEntityBindingSource.ResetCurrentItem();
        this.alphaEntityBindingSource.MoveFirst();

// Here is what the designer did inside the InitializeComponent():

        this.alphaEntityBindingSource.DataSource = typeof(Data911.Booking.EntityClasses.AlphaEntity);

        this.lastNameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.alphaEntityBindingSource, "LastName", true));

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 08-Mar-2007 05:47:00   

Just try the following:


 m_AlphaEntity = new AlphaEntity(hAlpha);
            if (m_AlphaEntity.IsNew)
                throw new System.ApplicationException("The hAlphaKey " + hAlpha.ToString() + " not found.");

            // bind the controls on the form:
            this.lastNameTextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", m_AlphaEntity, "LastName", true));

Andrei
User
Posts: 7
Joined: 19-Jan-2007
# Posted on: 08-Mar-2007 23:20:34   

Thanks Walaa, although, it does not seem like a perfect solution. You are suggesting to rebind controls every time the object data changes. This is going to affect the performance, not to mention the maintenance issue. What is the binding to type then for? Anyways, I suppose I can change my code to reuse the object, and get the new content using FetchUsingPK method. I have 2 questions in this regard:

  1. If I reuse the object using Fetch, will it nullify the related objects that were fetched into the previous instance via navigation?

  2. I suppose it is more efficient than creating a new object, by how much? Is there any guidance on one method vs. the other?

Thank you for your help.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 09-Mar-2007 08:45:42   

You are suggesting to rebind controls every time the object data changes.

I didn't mean that, just use the TextBox.Databindings.Add() method in the load or initialization of the form.

If I reuse the object using Fetch, will it nullify the related objects that were fetched into the previous instance via navigation?

If you have a graph of objects and you re-fetched an object within that graph, only the fetched object will be updated and the rest will be left as is.

Andrei
User
Posts: 7
Joined: 19-Jan-2007
# Posted on: 12-Mar-2007 04:55:00   

Thanks for your help Walaa. I figured it out. When you bind to a data type, the binding source is still a collection. Here is the way to do it, that works for me (in case someone else runs into the same problem):

         if (this.alphaEntityBindingSource.Count == 0)
            this.alphaEntityBindingSource.Add(m_AlphaEntity);
        else
            this.alphaEntityBindingSource[0] = m_AlphaEntity;
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 12-Mar-2007 07:30:01   

Thanks for the feedback