Enitity data binding with WinForms/Best practices

Posts   
 
    
Posts: 8
Joined: 24-Nov-2006
# Posted on: 05-Jan-2007 10:46:59   

Hi

Is there any best practices when databinding entities in WinForms applications?

I have created an user control for maintaining an entity. I wan't to reuse the enitity, because the entity might be shared with other user controls.

I came up with the solution shown below. The primary key is entered in one or more unbound controls (in the example selskabNrBE) and when the key is complete the entity is fetched and the properties shown in the bound controls.

In my user control constructor I create the entity and the binding source and sets up the data binding.

In the key complete event (in the example selskabNrBE_OnLeave) I use the Reset method descriped in the tread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5781&HighLight=1 in order to clear the entity. After fetching I call the ResetCurrentItem method on the binding source, in order to update the UI.

Is there any "one liners" for clearing all entity collections on the entity or do I have to implement it my self?

Why is it nessecary to call ResetCurrentItem method on the binding source, in order to update the UI after fetching the entity?


    private SelskabEntity _selskabEntity;
    private BindingSource _selskabBindingSource;

    public SelskabUserControl()
    {
      InitializeComponent();
      _selskabEntity = new SelskabEntity();
      _selskabBindingSource = new BindingSource();
      _selskabBindingSource.DataSource = _selskabEntity;
      selskabNavnLabel.Text = "";
      selskabNavnTextEdit.DataBindings.Add("Text", _selskabBindingSource, "SelskabNavn");
      adresse1TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse1");
      adresse2TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse2");
      postNrTextEdit.DataBindings.Add("Text", _selskabBindingSource, "PostNr");
      admStartDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStartDato");
      admStopDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStopDato");
    }


    private void selskabNrBE_Leave(object sender, EventArgs e)
    {
      if (((ButtonEdit)sender).IsModified)
      {
        int selskabNr = Convert.ToInt32(((ButtonEdit)sender).Text);
        _selskabEntity.Reset();
        _selskabEntity.SelskabNr = selskabNr;
        using (DataAccessAdapter adapter = new DataAccessAdapter())
          adapter.FetchEntity(_selskabEntity);
        selskabNavnLabel.Text = _selskabEntity.SelskabNavn;
        _selskabBindingSource.ResetCurrentItem();
      }
    }

Regards, Nikolaj Halvorsen

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Jan-2007 15:57:59   

Try to replace: _selskabBindingSource.ResetCurrentItem(); with: _selskabBindingSource.DataSource = _selskabEntity;

If it doesn't work, try the following code:


    private SelskabEntity _selskabEntity;
    private BindingSource _selskabBindingSource;

    public SelskabUserControl()
    {
     InitializeComponent();

     _selskabEntity = new SelskabEntity();
     _selskabBindingSource = new BindingSource();

     selskabNavnLabel.Text = "";

     BindData();
    }

    private void selskabNrBE_Leave(object sender, EventArgs e)
    {
     if (((ButtonEdit)sender).IsModified)
     {
        int selskabNr = Convert.ToInt32(((ButtonEdit)sender).Text);
        _selskabEntity.Reset();

       //_selskabEntity = new SelskabEntity(); // I'd have used this line

        _selskabEntity.SelskabNr = selskabNr;
        using (DataAccessAdapter adapter = new DataAccessAdapter())
         adapter.FetchEntity(_selskabEntity);
        selskabNavnLabel.Text = _selskabEntity.SelskabNavn;
        
        BindData(); 
     }
    }

    private void BindData()
    {
     _selskabBindingSource.DataSource = _selskabEntity;

     ClearBindings();

     selskabNavnTextEdit.DataBindings.Add("Text", _selskabBindingSource, "SelskabNavn");
     adresse1TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse1");
     adresse2TextEdit.DataBindings.Add("Text", _selskabBindingSource, "Adresse2");
     postNrTextEdit.DataBindings.Add("Text", _selskabBindingSource, "PostNr");
     admStartDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStartDato");
     admStopDatoTextEdit.DataBindings.Add("Text", _selskabBindingSource, "AdmStopDato");
    }

    private void ClearBindings()
    {
     selskabNavnTextEdit.DataBindings.Clear();
     adresse1TextEdit.DataBindings.Clear();
     adresse2TextEdit.DataBindings.Clear();
     postNrTextEdit.DataBindings.Clear();
     admStartDatoTextEdit.DataBindings.Clear();
     admStopDatoTextEdit.DataBindings.Clear();
    }

Also check the commented line before the fetch, I'd re-instanciated the entity before fetching it, rather than reseting it.

Posts: 8
Joined: 24-Nov-2006
# Posted on: 10-Jan-2007 07:58:54   

I have decided to create a new instance of the entity instead of reusing it. But I still a bit confused about the data binding.

I share the entity between two (or more) user control because of the very large number of properties. Each user control uses its own binding source (I get a lot of errors when trying to use the same binding source). A maximum of one user control is visible at any given time. The problem is that the user control not visible dosn't show the change of entity when it's shown even though I'm using the RestCurrentItem approach when the user control is shown.

I have tried dfifferent approaches and the only one working is the one suggested above: clearing the databindings and creating them again.

Is it considered best practice to recreate the data bindings? I think it's would be a much more pretty solution just to define the data bindings once for the binding source and only changing the data source of the binding source instead.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Jan-2007 15:27:15   

As far as I know, that's how it works in windows apps databinding.

I think the idea is that: since you reset the source of your dataSource (re-assign it to a new object), then you should re-do the bindings again.

jaschag
User
Posts: 79
Joined: 19-Apr-2006
# Posted on: 10-Jan-2007 16:47:09   

Walaa wrote:

As far as I know, that's how it works in windows apps databinding.

I think the idea is that: since you reset the source of your dataSource (re-assign it to a new object), then you should re-do the bindings again.

Not sure that is true - changing the datasource on the bindingsource should do the job. If you are having trouble with invisible usercontrols not reflecting changes after setting a new datasource, you could try overriding OnVisibleChanged on the usercontrols and call one of the other Reset... methods on the local bindingsource.

Posts: 8
Joined: 24-Nov-2006
# Posted on: 10-Jan-2007 18:45:42   

jaschag wrote:

Not sure that is true - changing the datasource on the bindingsource should do the job. If you are having trouble with invisible usercontrols not reflecting changes after setting a new datasource, you could try overriding OnVisibleChanged on the usercontrols and call one of the other Reset... methods on the local bindingsource.

Thanx - I'll try that.

Posts: 8
Joined: 24-Nov-2006
# Posted on: 11-Jan-2007 11:09:46   

It does the trick simple_smile